Rob
SCJP 1.4
"Thanks to Indian media who has over the period of time swiped out intellectual taste from mass Indian population." - Chetan Parekh
SCJP2. Please Indent your code using UBB Code
Rob "string literal mystery solver"
Originally posted by Jose Botella:
newString.intern() is not a string literal.
"Thanks to Indian media who has over the period of time swiped out intellectual taste from mass Indian population." - Chetan Parekh
SCJP2. Please Indent your code using UBB Code
intern
public String intern()
Returns a canonical representation for the string object.
A pool of strings, initially empty, is maintained privately by the class String.
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.
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.
All literal strings and string-valued constant expressions are interned. String literals are defined in �3.10.5 of the Java Language Specification
Returns:
a string that has the same contents as this string, but is guaranteed to be from a pool of unique strings.
Rob
SCJP 1.4
I don't expect questions regarding the reachability of string literals in the exam
FOR a time being .... if possible then anyone
what is going on here
what is the difference betn LINE 01 and LINE 02.
would luv to hear from jose.
(Yes it can be put in to Adv Java, but then you should provide some mechanism to get informed to other threads also if some reply comes.)
TIA
"Thanks to Indian media who has over the period of time swiped out intellectual taste from mass Indian population." - Chetan Parekh
Originally posted by Rob Ross:
Jose,
The main confusion I am having about the "string pool" is that there seems to be two of them, one for interned strings, and one for string literals, even though I can't find *any* literature that explains the difference.
Even the javadocs for the String.intern() method suggest there is only one pool...
If what you are saying is true, it seems the javadoc for String.intern() is incorrect?
"Thanks to Indian media who has over the period of time swiped out intellectual taste from mass Indian population." - Chetan Parekh
SCJP2. Please Indent your code using UBB Code
Rob
SCJP 1.4
I only state that string literals and String expressions computed at compile time (JLS 15.28) are not g.c.ed
"Thanks to Indian media who has over the period of time swiped out intellectual taste from mass Indian population." - Chetan Parekh
SCJP2. Please Indent your code using UBB Code
SCJP2. Please Indent your code using UBB Code
Rob
SCJP 1.4
Originally posted by Rob Ross:
String myString = "I am a string"+" composed of two segments, but I am one string literal";
int i = 5;
String aString = "This is a string literal "+i;
aString above, is NOT a string literal because its value must be computed at runtime. There is only one string literal above, and that is the text between the quotes.
Anyway, thanks for shedding light on this matter once and for all Jose!
Rob
"Thanks to Indian media who has over the period of time swiped out intellectual taste from mass Indian population." - Chetan Parekh
Can you shoot lasers out of your eyes? Don't look at this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
|