• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

what is the difference between String and other reference types?

 
Pavithra Hennayaka
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
we don't create objects of String using new keyword but to create objects of other reference type new is used.so what is the difference between other reference types and String?
 
Winston Gutkowski
Bartender
Pie
Posts: 10527
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pavithra Hennayaka wrote:we don't create objects of String using new keyword but to create objects of other reference type new is used.so what is the difference between other reference types and String?

First, your statement is incorrect; you can create Strings using new, it's just not advisable in many cases.

And the basic answer to your question is: essentially, nothing. A String is a reference type; the only thing the language has done is to allow a few extra goodies such as literals and the overloaded '+' operator to make them easier to use. There's also the String pool, but String isn't the only class to have cached values.

Winston
 
James Boswell
Bartender
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Pavithra Hennayaka
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for reply.
 
Rob Spoor
Sheriff
Pie
Posts: 20669
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String is only special because otherwise you would have to do a lot of extra work. Consider the following example:
If String weren't special, you would have to write the following:
As you see that looks bad, and it takes too long to write. Therefore, the creators of Java (and several other languages as well; even C has string literals even though it doesn't have a string type) decided that String will be like any other object, but with a few syntactic extras like String literals and String concatenation with +. They also added the String pool to prevent a new object to be created for each String literal. As far as I know, those are the only differences.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic