aspose file tools*
The moose likes Beginning Java and the fly likes String: Doubt in following program Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "String: Doubt in following program" Watch "String: Doubt in following program" New topic
Author

String: Doubt in following program

Gireesh Giri
Greenhorn

Joined: Apr 23, 2010
Posts: 23
Hi All,
Can any one explain, why i am getting the below mentioned output for the following program:

public class StringTest1 {

public static void main(String[] args) {

String str1= "a";
String str2 = str1;

System.out.println(str1);
System.out.println(str2);

str1 = str1+"b";
str2 = str2+"b";

System.out.println(str1);
System.out.println(str2);

if(str1 == str2)
System.out.println("equal");
else
System.out.println("not equal");


if(str1.equals(str2))
System.out.println("equal");
else
System.out.println("not equal");

}
}

/* out put:
a
a
ab
ab
not equal
equal
*/

NOTE: i am expecting not not equal also "equal" as per the String Constant pool mechanism.

Thanks & Regards,
Gireesh

R. Jain
Ranch Hand

Joined: Aug 11, 2012
Posts: 375
    
    1

Gireesh Chetty wrote:Hi All,

By comparing with ==, you are actually comparing the bit sequence of the reference str1 and str2, which are different in this case..
You can figure out why..
Gireesh Giri
Greenhorn

Joined: Apr 23, 2010
Posts: 23
Hi Jain,
Can you explain me the following two programs out put

1. public class StringTest1 {

public static void main(String[] args) {

String str1= "a";
String str2 = "a";


System.out.println(str1);
System.out.println(str2);

if(str1 == str2)
System.out.println("equal");
else
System.out.println("not equal");


if(str1.equals(str2))
System.out.println("equal");
else
System.out.println("not equal");

}
}

/* out put:
a
a
equal
equal
*/



2.
public class StringTest1 {

public static void main(String[] args) {

String str1= "a";
String str2 = "a";

System.out.println(str1);
System.out.println(str2);

str1 = str1+"b";
str2 = str2+"b";

System.out.println(str1);
System.out.println(str2);

if(str1 == str2)
System.out.println("equal");
else
System.out.println("not equal");


if(str1.equals(str2))
System.out.println("equal");
else
System.out.println("not equal");

}
}

/* out put:
a
a
ab
ab
not equal
equal
*/
R. Jain
Ranch Hand

Joined: Aug 11, 2012
Posts: 375
    
    1

Gireesh Chetty wrote:Hi Jain,
Can you explain me the following two programs out put


Let us see what happens when a .java file is compiled by the compiler: -
a). The compiler will search for all the distinct string literals in your java code, and check whether that literal is already present in Heap
b). If the literal is not present in heap, it creates one, and add a reference to that literal in the String literal constant pool..
c). Now any reference to that literal further in your program will be replaced by the reference you added in the constant pool..

So, in this case, you are actually placing one object on heap and both your local variables are assigned a reference to that object..

Now consider your second code...

When you write: -
str = "Hello" and
str = new String("Hello");


You are actually creating two different object on heap both containing the same content..
Similar is the case with your this code: -

Here, str1 and str2 points to two different objects on the heap with the same content "ab"
Thus the result..
R. Jain
Ranch Hand

Joined: Aug 11, 2012
Posts: 375
    
    1

For more detail see JLS for String literals..
Gireesh Giri
Greenhorn

Joined: Apr 23, 2010
Posts: 23
Hi Jain,
If my understanding is correct. it means, if i am using + operator or conCat method. The new String object which it will get create, is similar to new String("Hello");.
Please correct me if i am wrong.
R. Jain
Ranch Hand

Joined: Aug 11, 2012
Posts: 375
    
    1

Gireesh Chetty wrote:Hi Jain,
If my understanding is correct. it means, if i am using + operator or conCat method. The new String object which it will get create, is similar to new String("Hello");.
Please correct me if i am wrong.

Yeah you got it right :thumbup:
Gireesh Giri
Greenhorn

Joined: Apr 23, 2010
Posts: 23
Thanks Jain it helps me a lot :) .
 
jQuery in Action, 2nd edition
 
subject: String: Doubt in following program