Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Confused with String creation.

 
Rafa Barcel� Bauz�
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello.

I'm very confused with String creation since i saw this:

1- String s1 = "abc";
2- String s2 = "ab";
3- s2 = s2 + "c";

the result is s1 != s2!!! Why?

Let's supose String constant pool is empty...

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... :-(
 
Shang Lee
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rafa,


1- String s1 = "abc";
2- String s2 = "ab";
3- s2 = s2 + "c";

the result is s1 != s2!!! Why?


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 ]
 
Rafa Barcel� Bauz�
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Shang Lee
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rafa,


so s2 should refer to the same object refered by s1 in the String Constant pool.


You can try looking at the problem this way:


1- String s1 = "abc";
2- String s2 = "ab";
3- s2 = s2 + "c";


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.

Best Wishes,
Shang
 
Dave Reinhardt
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
According to SCJP p.413:

String s = "abc";

is just short hand for:

String s = new String("abc");

and both will create a new object.

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.
 
wise owen
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
also check this thread.
 
sathis gopal
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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."

The above explanation will solve your doubt
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic