SCJP 1.4<br />" Something is difficult doesn't mean you shouldn't try, it only means you should try harder "
sandy
sandy
This example illustrates six points:
Literal strings within the same class (�8) in the same package (�7) represent references to the same String object (�4.3.1).
Literal strings within different classes in the same package represent references to the same String object.
Literal strings within different classes in different packages likewise represent references to the same String object.
Strings computed by constant expressions (�15.28) are computed at compile time and then treated as if they were literals.
Strings computed at run time are newly created and therefore distinct.
The result of explicitly interning a computed string is the same string as any pre-existing literal string with the same contents.
Tony Morris
Java Q&A (FAQ, Trivia)
yes & in this case there are two objects getting created , one is on GCH & one is on SLP
Regards,<br />Subhash Bhushan.
Tony Morris
Java Q&A (FAQ, Trivia)
Cheers,<br />Jay<br /> <br />(SCJP 1.4)<br />Heights of great men were not achieved in one day, they were toiling day and night while their companions slept.
Mike Gershman
SCJP 1.4, SCWCD in process
Although that's close, it's not exactly correct. Really, it's a collection of references to String objects
If someone can point to the spec where I'm wrong on that, let me know.
Tony Morris
Java Q&A (FAQ, Trivia)
"I'm not back." - Bill Harding, Twister
"I'm not back." - Bill Harding, Twister
All string are yet refenced by the Pool String and they will never be elegible for GC, period.
Tony Morris
Java Q&A (FAQ, Trivia)
� Literal strings within the same class (�8) in the same package (�7) represent references to the same String object (�4.3.1).
� Literal strings within different classes in the same package represent references to the same String object.
� Literal strings within different classes in different packages likewise represent references to the same String object.
� Strings computed by constant expressions (�15.28) are computed at compile
time and then treated as if they were literals.
� Strings computed at run time are newly created and therefore distinct.
� The result of explicitly interning a computed string is the same string as any pre-existing literal string with the same contents.
...prove that X.s and Y.s can refer to two different instances during runtime...
Tony Morris
Java Q&A (FAQ, Trivia)
20.12.47 public String intern()
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 method (�20.12.9), 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 (�3.10.5).
"I'm not back." - Bill Harding, Twister
Tony Morris
Java Q&A (FAQ, Trivia)
"I'm not back." - Bill Harding, Twister
"I'm not back." - Bill Harding, Twister
"I'm not back." - Bill Harding, Twister
An implementation of the Java programming language may unload classes. A
class or interface may be unloaded if and only if its defining class loader may be reclaimed by the garbage collector as discussed in �12.6. Classes and interfaces loaded by the bootstrap loader may not be unloaded.
Tony Morris
Java Q&A (FAQ, Trivia)
Consider Paul's rocket mass heater. |