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

Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "doubt in using String" Watch "doubt in using String" New topic

doubt in using String

Vaibhav Chauhan
Ranch Hand

Joined: Aug 16, 2006
Posts: 115
Check the following 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);

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)

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

Joined: Feb 02, 2006
Posts: 2023

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

Joined: Aug 26, 2006
Posts: 4968

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

Joined: Aug 03, 2002
Posts: 7729
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 ]

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Vaibhav Chauhan
Ranch Hand

Joined: Aug 16, 2006
Posts: 115
thanks to all ..... now i m clear.
I agree. Here's the link:
subject: doubt in using String
Similar Threads
Behaviour of String and StringBuffer class.
how many String objects are created
How many String Object are created in the following code ??
String is not working as i supposed it to work
Null String Concat