This week's book giveaway is in the General Computing forum.
We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line!
See this thread for details.
The moose likes Java in General and the fly likes Comparing (==) strings Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Comparing (==) strings" Watch "Comparing (==) strings" New topic
Author

Comparing (==) strings

Nidhi Sar
Ranch Hand

Joined: Oct 19, 2009
Posts: 252

I have a question about how the == operator & toString() method are implemented for String & other objects.

Please take a look at the following code:

The result is the following:
True 2

I am a little confused that the condition 1 is false, but condition 2 is true. Can anyone explain this?

Thanks,
- Nidhi


"A problem well stated is a problem half solved.” - Charles F. Kettering
SCJP 6, OCPJWCD
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32827
    
    4
Because Strings are cached. Any Strings which are compile-time constants are put into a special part of the heap, and if two compile-time constant Strings are identical, the JVM reuses the same String object.

And what do you think will happen if you call toString() on a String?
Nidhi Sar
Ranch Hand

Joined: Oct 19, 2009
Posts: 252

Hi Campbell, Thanks for taking the time to answer my question!

Campbell Ritchie wrote:Because Strings are cached. Any Strings which are compile-time constants are put into a special part of the heap, and if two compile-time constant Strings are identical, the JVM reuses the same String object.


Is this cache the same thing as "String pool". But in that case when you compare (c.toString() == c.toString()), the second time toString() runs [the right-hand side argument], it should refer back to the same String in the String pool created an instant back by the left-hand side argument. Shouldn't it?

Campbell Ritchie wrote:And what do you think will happen if you call toString() on a String?

toString() method would normally create a new String object, but not if you call toString() on a String. someString.toString() would simply return back the same someString. Right?


anand chouti
Greenhorn

Joined: May 10, 2004
Posts: 5
c.toString() will actually create a new String everytime, it doesn't use the string from the string pool (The general concept in strings is whenever we use String s = new String("something"), there is a new object created in heap irrespective of the value passed, even if the second string has the same value as the first one. But when we use String s = "something" and String str = "something", in such case it uses same string in the pool)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 16809
    
  19

Actually, the string pool is not related to the answer here.... With strings, the toString() method always return the string object. So ... this...



Is the equivalent to this....



It prints true because they are the same instance, which is true because the same reference was used for both sides of the condition.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Nidhi Sar
Ranch Hand

Joined: Oct 19, 2009
Posts: 252

Thanks, Anand, Henry.... That makes a lot of sense!!!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32827
    
    4
You have got it worked out, I see

You can confirm that someString.toString() returns someString unchanged by the simple technique of opening the src.zip file and looking for the String class (or even going to the API documentation where it tells you "this object is returned." The code looks like this
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19230

There is a file called "src.zip" (the one Campbell mentioned) in your JDK installation folder. It includes the source code for most of the Java API. If you would look at that you would see that String.toString() indeed returns "this". Character.toString() on the other hand returns the following in Java 6:
So we check out String.valueOf(char[]):
So yes, that creates a new String object every time.


SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
Embla Tingeling
Ranch Hand

Joined: Oct 22, 2009
Posts: 237
Nidhi Sar wrote:I am a little confused that the condition 1 is false, but condition 2 is true. Can anyone explain this?


The result of condition 2 follows from the definition of toString of String in the Java API documentation..

The result of condition 1 on the other hand is undefined and this follows from the definition of toString of Character in the Java API documentation. This means no one can predict the outcome of condition 1. You'll have to look at the actual API implementation you're using.

To avoid confusion and bugs in the future, never compare Strings using ==. Use equals instead.
Embla Tingeling
Ranch Hand

Joined: Oct 22, 2009
Posts: 237
Rob Prime wrote:There is a file called "src.zip" (the one Campbell mentioned) in your JDK installation folder. It includes the source code for most of the Java API. If you would look at that you would see that String.toString() indeed returns "this". Character.toString() on the other hand returns the following in Java 6:


That's pretty useless. The Java API is defined by its documentation, not by a specific implementation. Any observed behaviour that's not defined in the API is undefined and can vary between implementations.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Comparing (==) strings
 
Similar Threads
Generic question
Strings
Placing text into a BufferedImage
compilation error
wrapper class