File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes String class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


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

String class

Roger Chan
Ranch Hand

Joined: Nov 13, 2001
Posts: 30
class TestClass
{
public static void main(String args[])
{
String str1 = "str1";
String str2 = "str2";
String str3 = str1;
str1 = str1.concat(str2);

System.out.println( str1);
System.out.println( str3);
}
}
As String class is immutable, a new object is created when the content of the String instance changes. But in the above code snippet, str1 concatenate with str2. The result string is assigned backed to str1(same intance name). Will this str1 be a new object or still be the original str1 which contains "str1"?
Thanks
vishal avad
Ranch Hand

Joined: Nov 29, 2001
Posts: 45
hi roger
u must understand that assignment has lower precision so firstly
str1 and str2 holding initial values get concated then the result is trying to store values in str1.
as string pool does not hold string object like "str1str2" so new object is created and str1 starts pointing to it.
rgds
vishal
Darryl Failla
Ranch Hand

Joined: Oct 16, 2001
Posts: 129
After the operations are performed, str1 points to a NEW concatinated String, "str1str2". Your code verifies this. Before the concat() operation is performed, str1 and str3 both point to the same String. If the concat() operation had changed the data within the String, str1 and str3 would still point to the same String after the concat() command.
When you run the code, however, you will see that it does not. str1 points to the new concatinated String, and str3 still points to the original String.


Darryl Failla
Sun Certified Java 2 Programmer
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Originally posted by vishal avad:

as string pool does not hold string object like "str1str2" so new object is created and str1 starts pointing to it.

Vishal,
even if the String pool had a String "str1str2" a new String would have been created because of the way String objects are concatenated.
The concat method returns a new String object (created with new).
And Roger,
you must understand the difference between a reference to a String object (here str1, str2 and str3) and the actual String object. str1 is not the instance name of the String referenced by str1, BUT the name of the variable that references it. It's important to make that distinction. Reference variables are in fact pointers to objects. When you create a new object (with new), there is only one instance of that object, but you may create mutliple reference to that object using reference variables. It is the case here with str1 and str3 which both reference the same String instance "str1" ! That does not mean that there are two instances of "str1" !
HIH
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
[This message has been edited by Valentin Crettaz (edited November 30, 2001).]


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String class