This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of Real-World Software Development: A Project-Driven Guide to Fundamentals in Java and have Dr. Raoul-Gabriel Urma & Richard Warburton on-line!
See this thread for details.
Win a flower (🌹) or copy of Real-World Software Development: A Project-Driven Guide to Fundamentals in Java (📚) this week in the Agile and Other Processes forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Liutauras Vilda
  • Knute Snortum
  • Bear Bibeault
master stewards:
  • Devaka Cooray
  • Jeanne Boyarsky
  • Junilu Lacar
garden masters:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • salvin francis
gardeners:
  • Tim Holloway
  • Piet Souris
  • Frits Walraven

Yet another string pool doubt

 
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?
 
Marshal
Posts: 68009
258
  • 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
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: 68009
258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't know. Sorry. Maybe somebody else will be able to help.
 
gardener
Posts: 5167
11
Netbeans IDE Opera Java
  • 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
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • 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
    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?
     
    pollinator
    Posts: 3437
    35
    • 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
    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.
     
    To do a great right, do a little wrong - shakepeare. twisted little ad:
    Java file APIs (DOC, XLS, PDF, and many more)
    https://products.aspose.com/total/java
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!