Richard Hayward wrote:Could anyone tell me why not?
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.
Hope it helps!
Richard Hayward wrote:Is there a difference, with respect to GC, between strings placed in the pool as a result of a source code literal and those placed there as a result of intern()?
All literal strings and string-valued constant expressions are interned. String literals are defined in section 3.10.5 of the The Java™ Language Specification.
Roel De Nijs wrote:
Again from the javadoc of the intern() methodAll literal strings and string-valued constant expressions are interned. String literals are defined in section 3.10.5 of the The Java™ Language Specification.
Roel De Nijs wrote:
Luckily you don't need to worry about strings and GC on the actual exam...
Richard Hayward wrote:Ok, literals are interned, but I'm wondering if interned literals are still eligible for GC.
SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Tobias Bachert wrote:They are eligible for GC if the corresponding Class (and Classloader) is eligible for GC, i.e. the following prints true, false, true:
salman khandu wrote:
If I removed Line-2 and compare s1==s2, it will return true. Could anyone explain me what exactly happens in string pool after Line-2?
Bhavin Akolkar wrote:1) String s3 = new String("HelloWorld"); will return false for line System.out.println(s1 == s2);
2) String s3 = new String(s1); will return true for line System.out.println(s1 == s2);
What is the difference?
Can anyone please explain?
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime. |