Originally posted by Jim Yingst:
We could clear it again, I suppose, but that would just be followed by another round of people asking questions without reading and understanding the preceding posts...
[ April 04, 2005: Message edited by: Jim Yingst ]
Be Afraid...Be very Afraid...
Tony Morris
Java Q&A (FAQ, Trivia)
Creates 3 objects (2 from the String pool and 1 from the constructor)
Creates 2 objects (1 from the String pool and 1 from the constructor)
makes
"E"
"F"
"G"
"EF"
"EFG"
AND the object created by new...which is SIX not four.
Be Afraid...Be very Afraid...
"I'm not back." - Bill Harding, Twister
"I'm not back." - Bill Harding, Twister
String literals-or, more generally, strings that are the values of constant expressions (�15.28)-are "interned" so as to share unique instances, using the method String.intern.
"I'm not back." - Bill Harding, Twister
String four = new String("E" +"F" + "G");System.out.println(four);
--------------------------------------------------------------------------------
Number of objects: 6
quote:
--------------------------------------------------------------------------------
makes
"E"
"F"
"G"
"EF"
"EFG"
AND the object created by new...which is SIX not four.
--------------------------------------------------------------------------------
**********************************
code:
--------------------------------------------------------------------------------
String s1 = "aa";String s2 = "a" + "a";
--------------------------------------------------------------------------------
Number of objects: 2
"aa" and "a"
Tony Morris
Java Q&A (FAQ, Trivia)
SCJP 1.4, SCBCD 1.3
"I'm not back." - Bill Harding, Twister
SCJP 1.4<br />SCJD <br />SCWCD (Studying)
"I'm not back." - Bill Harding, Twister
Originally posted by Jim Yingst:
[C]: I cant think of a valid use for the first syntax.
Such syntax is generally only used in order to construct Garbage Collection problems, in order to bypass the issue of sting pooling. (Sometimes successfully, sometimes not.) Other than that, there is only one good reason to ever use a new String(String) constructor, and it's extremely rare. So rare that it will never ever appear on any SCJP exam, and I will leave it as an excercise for any interested parties to discover.
[ April 07, 2005: Message edited by: Jim Yingst ]
SCJP 1.4<br />SCJD <br />SCWCD (Studying)
Tony Morris
Java Q&A (FAQ, Trivia)
"I'm not back." - Bill Harding, Twister
The overall mission is to change the world. When you've done that, then you can read this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
|