• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

doubt in using String

 
Vaibhav Chauhan
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check the following code,

code:

public static void main(String []args)
{
String s1=new String();
String s2=s1; // line1
System.out.println("s1 and s2 are same:" + (s1==s2));
s1=s1+s2; // line 2
System.out.println("after concatenation, s1 is: "+s1);
}

output:
s1 and s2 are same:true
after concatenation, s1 is:


I have two doubts.
(1) As I know we can't use local reference variables (i.e. s1 and s2) before their initialization. But here we are concatinating s1 and s2 without initializing either.(at line 2)

(2)
As I know String class is immutable so any change in s1 will leave s2 unaffected. I have confusion that if content of s1 is changed, then only a different object of String is created or two different objects are created at line 1 itself, which i don't think is the case otherwise output would have been:
//s1 and s2 are same:false

please help me.
 
wise owen
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

public static void main(String []args)
{
String s1=new String(); // Line A
String s2=s1; // line1 // There is only one String created and refered by s1 and s2
System.out.println("s1 and s2 are same:" + (s1==s2));
s1=s1+s2; //line2 ---> You had initialized them in Line A & Line 1
System.out.println("after concatenation, s1 is: "+s1);
}
 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When creating a String, the JVM looks for matching character strings, and if it finds a matching character string in memory, it will store the string in that memory location, essentially sharing that memory location. As a result, two strings, s1 and s2, named "Joe" will be == to each other, since they share the same memory location. Add an s3 named "Joe" and again, they will all be == to each other.

The exception is using the new keyword with a String - this always creates a new memory location, regardless of whether there is a matching characters string somewhere.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Notice that new String() does create a string - it creates an "empty string" - one with no characters. So s1 does refer to a string (that is s1 != null) and s.length() is zero. System.out.println(s1) does not print "null" as it would if s1 was uninitialized, it prints nothing because there is nothing in the string to be printed.

s1 + s2 results in another string with no characters.

s1 = s1 + s2 reassigns the reference s1 to refer to the new empty string.
s2 still refers to the original empty string.
[ August 28, 2006: Message edited by: Barry Gaunt ]
 
Vaibhav Chauhan
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks to all ..... now i m clear.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic