• Post Reply Bookmark Topic Watch Topic
  • New Topic

String constant pool  RSS feed

 
cleverson teo
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm reading the OCA Java SE 8 Programmer I Exam Guide (Exams 1Z0-808) (Certification & Career - OMG) by Kathy Sierra and Bert Bates, where I found the following:

In the next case, because we used the new keyword, java will create a new String object in normal (nonpool) memory, and s will refer to it. In addition, the literal "abc" will be placed in the pool:


My problem is with the last sentence, the pool part. I conclude that if this is true, so the following code should return 'true true', but it returns 'false true'. Leading me to believe that the String literal used as an argument to the constructor wasn't placed on the pool.



What am I missing here?
 
Santosh Kumar Nayak
Ranch Hand
Posts: 193
Eclipse IDE IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try Out this :-

This makes your code point to pool literal

System.out.println("s0.equals(s1) -> " + s0.intern().equals(s1));
 
Santosh Kumar Nayak
Ranch Hand
Posts: 193
Eclipse IDE IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This will clear all your doubts 

 
cleverson teo
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unfortunately, I don't think this works. The Java API states the following:
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.

As can be seen in:
String's intern method

So, if I interpreted right, your code get the String from the pool before do the comparison. We are left unable to know if the literal from the new statement is or not on the pool. It can be a String with the same content out of the pool. However, I think I got it. The literal from the object is placed in a nonpool memory and then other literal that is a copy of it (not the same), is added to the pool.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
cleverson teo wrote:
My problem is with the last sentence, the pool part. I conclude that if this is true, so the following code should return 'true true', but it returns 'false true'. Leading me to believe that the String literal used as an argument to the constructor wasn't placed on the pool.



What am I missing here?


Line 3, the reference that is passed to the constructor is in the string pool. The reference that is assigned to s0, is new, and hence, not in the string pool. And lastly, Line 4, the reference that is assigned to s1 is in the string pool.

BTW, why do you think that the result should be "true true"?

Henry
 
cleverson teo
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Basically because I misunderstood the concept. I thought that the address of the literal would be assigned either to the new object and to the pool and I forgot that the literal itself is an object and not a char array.
My interpretation was that a new String wold be created with the content "Test" and assigned to s0, then this String (with the same memory address) wold be placed on the pool. So, when I declare s1 the JVM wold find the literal on the pool and assign it (the same memory address) to s1. But, if I got right this time, the literal argument "Test" is the string that goes to the pool. A copy of it (a new object) is made inside the line 3 object constructor and this copy (the new object) is not on the pool. Right?
 
Fred Kleinschmidt
Bartender
Posts: 571
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you say

then the first thing that happens is "Test" is added to the string pool if it is not already there. Then the reference s is set to point to that pool object.
When you write

the first thing  that happens is that again"Test" is added to the pool if it is not already there. But then String's constructor is called with that pool object as its argument. But the constructor has no way of knowing whether its argument is something from the pool something from some other String object, so it blindly creates a new character array and copies the argument's content into that space.

Consider this:

At this point the string "Test" is in the pool, and there are two other objects that both contain "Test" as their text, but all three occupy distinct and different space.
 
cleverson teo
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got it! Thanks very much to everyone.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!