1- Creates the String object "abc" in de String constant pool an s1 refers to it. 2- Creates the String object "ab" in de String constant pool an s2 refers to it.
3- Creates de "c" String which is lost soon, and creates de "abc". But this String is existing in the String constant pool..., so s2 should refer to it too.(Thats why String constant pool makes a more efficient use of memory).
then why s1 != s2 if both references should refer to the same String object in the String constant pool... , they wasn't created using "new String()"!!!
Im very confused, i thought i was understanding the String constant pool... :-(
s1 and s2 are essentially referring to two different objects so "System.out.println(s1==s2);" will return "false".
If you want the two references to show "true", use "System.out.println(s1.equals(s2));" instead because doing so means you are comparing their contents. [ August 31, 2006: Message edited by: Shang Lee ]
Best Wishes,<br />Shang
Rafa Barcel� Bauz�
Joined: Aug 09, 2006
Hy Shang lee.
But both s1 and s2 are created without using "new", so the "abc" created by s1 is placed directly in the String constant pool. Then at the end, we create another "abc" string not using "new", so the JVM, goes to the String constant pool to verify if "abc" is in it, and finds it, so s2 should refer to the same object refered by s1 in the String Constant pool.
Well, that is not happening since s1 != s2, but i still can understant why???
The target of String constant pool should be decrease use of memory by referencing s1 and s2 to "abc" String object, instead of creating a new Object.
Joined: Aug 17, 2006
so s2 should refer to the same object refered by s1 in the String Constant pool.
1- String s1 = "abc"; --------------------- "abc" String object is created and s1 refers it.
2- String s2 = "ab"; --------------------- "ab" String object is created and s2 refers it.
3- s2 = s2 + "c"; ----------------- Two new String objects are created here. The first is the "c" String object. You can consider this String object "c" as lost because no one refers to it. The second is the newly created "abc" String object which is now referred to by s2. There are now two "abc" String objects on the heap. Hope this helps.
As for how the constant pool works, I don't know. There must be a mechanism that tracks how many references to "abc" are present so the GC knows when it can delete the object. For the exam I think all you need to know is that anytime you assign/reassign a reference variable to a string a new string is created--and if nothing is referencing a string object, it is eligible for GC.
Hi Rafa, The statement you made looks conflicting to me.
JAVA API for String says
"The Java language provides special support for the string concatenation operator ( + ), and for conversion of other objects to strings. String concatenation is implemented through the StringBuffer class and its append method. String conversions are implemented through the method toString, defined by Object and inherited by all classes in Java. For additional information on string concatenation and conversion, see Gosling, Joy, and Steele, The Java Language Specification."