This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes '==' operator on String objects Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark " Watch " New topic
Author

'==' operator on String objects

SDave Bee
Greenhorn

Joined: Jun 04, 2003
Posts: 3
Hi All,
Could anyone please explain the reason for these answers
if("String".toString() == "String")
System.out.println("Equal");
else
System.out.println("Not Equal");
Answer:
the code will compile an print "Equal".
--------------------------------------------------------------------------------
if(" String ".trim() == "String")
System.out.println("Equal");
else
System.out.println("Not Equal");

Answer:
the code will compile an print "Not Equal".

--------------------------------------------------------------------------------
Thanks for any early response
SDave Bee..
Brian Joseph
Ranch Hand

Joined: May 16, 2003
Posts: 160
Well, the first one fooled me, because I thought toString() might return a new String object, which evidently is wrong. Since String objects are immutable, it will just return the same object, as indictaed in the docs. My guess it's just more efficient design that way.
The second one is really asking whether the methods of String return a String object from the constant string pool, if possible. The answer must be no, and I have not read anything which suggests that is the case. The only String method I know of which does this is String.intern(). It will return the String object from the pool if it exists.
String s1 = "Java";
String s2 = new String("Java").intern();
s1 == s2 -> true.
SDave Bee
Greenhorn

Joined: Jun 04, 2003
Posts: 3
Hi Joseph ,Thanks for ur reply ,it's really helped a lot for me.
But i come accross another question which is:
Byte b1 = new Byte("127");

if(b1.toString() == b1.toString())
System.out.println("true");
else
System.out.println("false");
Answer: false

Does this because of the IMMUTABLE NATURE of the wrapper classes? or we can only change the wrapper objects with "parseInt(String)"and such kind of methods ?
Thanks in advance for reply
SDave
Richard Jensen
Ranch Hand

Joined: May 14, 2003
Posts: 67
Originally posted by Brian Joseph:
Well, the first one fooled me, because I thought toString() might return a new String object, which evidently is wrong. Since String objects are immutable, it will just return the same object, as indictaed in the docs.

I think that in the first example, we have the case that the string literals are automatically intern()ed for you? Therefore the reference comparison is using the same values.


Richard
N 37 33 W 122 18
Richard Jensen
Ranch Hand

Joined: May 14, 2003
Posts: 67
Originally posted by SDave Bee:
Byte b1 = new Byte("127");

if(b1.toString() == b1.toString())
System.out.println("true");
else
System.out.println("false");
Answer: false

I think that unlike your first case where we are dealing with string literals, the toString() on the byte object _has_ to make a new string for each call and therefore the references differ.
Brian Joseph
Ranch Hand

Joined: May 16, 2003
Posts: 160
Don't forget that the wrapper classes to the primitve number types are immutable as well and can't be changed. There is no method which will change the value of an Integer object, for example.
There are only two constructors for each of these (one takes a string, the other takes the primitive of the wrapper "type").
SDave Bee
Greenhorn

Joined: Jun 04, 2003
Posts: 3
Thanks guys,thanks for the reply.
SDave
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
It is actually very simple. Any time that a method that works on a String has to change the contents of the String, it returns a brand new String object created on the heap. Any time that method that works on a String does not have to change the contents of the String, it returns the very same String, not a new one.
So we get this:
" String ".trim() == "String" returns false
"String".trim() == "String" returns true


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: '==' operator on String objects
 
Similar Threads
Strings
String
String Class
Please Answer soon with explanation
String literals