• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

String class

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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).]
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic