Roel De Nijs wrote:...I assume the String "java" is already in the String Literal Pool (for some reason) and therefore b.intern() returns a reference to that String without having to add String "java" to the String Literal Pool.
A pool of strings, initially empty, is maintained privately by the class String.
Returns:
a string that has the same contents as this string, but is guaranteed to be from a pool of unique strings.
Daniel Cox wrote:I doubt that the string "java" is already in the String Literal Pool since the API documentation for the intern() method says that the pool is initially empty.
Daniel Cox wrote:I suspect that the reason why the code snippets return false and true is that when we call b.intern(), the only guarantee the API makes is that b.intern() will return a string from the pool. The API does not guarantee that b will refer to a string in the pool.
Meaning if the String "java" does not yet exist in the pool (as in the 2nd example), the String object (also refered to by b) is added to the pool and that reference is returned and assigned to c, resulting b and c having the same reference.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.
Roel De Nijs wrote:I know the pool is empty, but that doesn't mean the String "java" can't be added for any reason...
Roel De Nijs wrote:...resulting b and c having the same reference.
Daniel Cox wrote:The code String c=b.intern() guarantees that c refers to the string "java" in the pool but does not guarantee that b refers to the same string "java" in the pool as demonstrated by the 1st example.
Roel De Nijs wrote:Why is "java" (and "main") already in the String Literal Pool
Roel De Nijs wrote:If the String "java" was not yet in the String Literal Pool, the String "java" would be added to the String Literal Pool and that reference would be returned resulting in b and c referring to the same object (according to the javadoc of the intern() method and illustrated with the 2nd code snippet).
Daniel Cox wrote:I think that the statement "resulting in b and c referring to the same object" is wrong.
Lilou Laure wrote:this is quite interesting !
Roel De Nijs wrote:So now you have three reference variables refering to the same String object: b, c and POOL[0] (assuming no other strings were already added to the pool).
Roel De Nijs wrote:
Lilou Laure wrote:this is quite interesting !
That's definitely interesting but please note that this is waaaaay beyond the scope for the OCAJP and OCPJP certification exams Have a cow for a great question!
Daniel Cox wrote:@Tobias - some ranchers might be interested in making your code compile.
Daniel Cox wrote:
Roel De Nijs wrote:So now you have three reference variables refering to the same String object: b, c and POOL[0] (assuming no other strings were already added to the pool).
You're assuming that b.intern() implies that b will refer to a string in the pool. This is not correct.
Roel De Nijs wrote:I wonder why you only quote one sentence from a three line (on my computer) paragraph - therefore get rid of all context I've provided (which is very important here) - and tell me I am wrong
Slime does not pay. Always keep your tiny ad dry.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
|