• Post Reply Bookmark Topic Watch Topic
  • New Topic

Yet another string pool doubt  RSS feed

 
Greg Parker
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Both the sysouts prints different values. Why is this? Doesnt this mean there is an already existing string "Differ" in the pool? how did that happen?
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

It says that System#identityHashCode(java.lang.Object) returns the same result as Object#hashCode(), which means that different hash code mean they are different objects, ie not returning true to the == operator.

Now you need to read what String#intern() does, whether it put the object into the String pool, or whether it creates a new object. I actually find what it says there a bit unclear.
 
Greg Parker
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Now you need to read what String#intern() does, whether it put the object into the String pool, or whether it creates a new object. I actually find what it says there a bit unclear.


"When the intern method is invoked, if the pool already contains a string equal to this String object as determined by the equals(Object) method, then the string from the pool is returned. Otherwise, this String object is added to the pool and a reference to this String object is returned"

is what the javadoc says. That is what I based my logic on. Is there any twist to it?
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't know. Sorry. Maybe somebody else will be able to help.
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greg Parker wrote:"When the intern method is invoked, if the pool already contains a string equal to this String object as determined by the equals(Object) method, then the string from the pool is returned. Otherwise, this String object is added to the pool and a reference to this String object is returned"

is what the javadoc says. That is what I based my logic on. Is there any twist to it?

I agree that the behavior does not appear to be as documented. But, the javadoc goes on to say
It follows that for any two strings s and t, s.intern() == t.intern() is true if and only if s.equals(t) is true.

That part of it is correct. You can see this if you change your code to
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16057
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greg Parker wrote:Both the sysouts prints different values. Why is this? Doesnt this mean there is an already existing string "Differ" in the pool? how did that happen?

Did you expect them to print the same value?

In line 3, you are creating a new String object. That String object will not be in the string pool (only string literals will be in the string pool automatically).

What s.intern() does, is put (a copy of) the string in the string pool and then return the reference to the String object that's in the pool.

  • Calling intern() on a string that is not in the pool, such as the string you created in line 3, will create a new String object that is in the pool, and return a reference to that new string that is in the pool.
  • Calling intern() on a string that is already in the pool will just return a reference to that same String object.


  • The first of these two is the case here. So there are two String objects here, the original one created in line 3, and a copy that's in the string pool, created by calling intern() on s in line 5. Since they are two distinct objects, they will have a different identityHashCode.
     
    Greg Parker
    Greenhorn
    Posts: 10
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Jesper Young wrote:

    What s.intern() does, is put (a copy of) the string in the string pool and then return the reference to the String object that's in the pool.



    But the javadoc says "this String object is added to the pool and a reference to this String object is returned. Any idea where we could get more info on the intern method other than for java doc?
     
    Mike Simmons
    Ranch Hand
    Posts: 3090
    14
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I asked a really smart friend about this a couple years ago, and he went to track this down in the JDK source. Not the Java source, but the C source, since intern() is a native method. Maybe he will post in detail about what he found. But the short answer is that yes, the JDK is making a copy, and yes, that's not what the JDK says should happen. It's also not what originally happened, with earlier versions of Java. But Sun changed this behavior back when they introduced the permanent generation region of heap space, and decided that interned Strings should be stored there as a performance operation. The easiest way to do this is to copy them there when intern() is called. It doesn't seem to cause any problems, other than changing the value returned by System.identityHashCode(). It's a violation of the spec that improves performance and causes no real problems, so we don't worry about it.
     
    Greg Parker
    Greenhorn
    Posts: 10
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thank you.. that was helpful.. I was going on to see which all strings would be in the pool when you append them. So this approach may not help me.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!