This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes what is the difference between String and other reference types? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "what is the difference between String and other reference types?" Watch "what is the difference between String and other reference types?" New topic
Author

what is the difference between String and other reference types?

Pavithra Hennayaka
Greenhorn

Joined: Feb 03, 2011
Posts: 2
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

Joined: Mar 17, 2011
Posts: 7554
    
  18

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


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
James Boswell
Bartender

Joined: Nov 09, 2011
Posts: 1012
    
    5

Pavithra Hennayaka
Greenhorn

Joined: Feb 03, 2011
Posts: 2
Thanks for reply.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19656
    
  18

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
 
subject: what is the difference between String and other reference types?
 
Similar Threads
string object
Difference between String
String
Difference between Reference Data Types & Own Objects
how many objects are created