• Post Reply Bookmark Topic Watch Topic
  • New Topic

Comparing (==) strings  RSS feed

 
Ranch Hand
Posts: 252
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 252
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?


 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)
 
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Nidhi Sar
Ranch Hand
Posts: 252
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Anand, Henry.... That makes a lot of sense!!!
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 237
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!