aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Confused with String creation. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Confused with String creation." Watch "Confused with String creation." New topic
Author

Confused with String creation.

Rafa Barcel� Bauz�
Greenhorn

Joined: Aug 09, 2006
Posts: 22
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

Joined: Aug 17, 2006
Posts: 20
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 ]

Best Wishes,<br />Shang
Rafa Barcel� Bauz�
Greenhorn

Joined: Aug 09, 2006
Posts: 22
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

Joined: Aug 17, 2006
Posts: 20
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

Joined: Aug 07, 2006
Posts: 54
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

Joined: Feb 02, 2006
Posts: 2023
also check this thread.
sathis gopal
Greenhorn

Joined: Aug 31, 2006
Posts: 1
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Confused with String creation.
 
Similar Threads
string constant pool
About Strings...
I am Confused !!!!! Pls Help
problem with assigning Strings to variables
How many String objects are created.