Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String: Doubt in following program

 
Gireesh Giri
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 375
1
Java Python Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 375
1
Java Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 375
1
Java Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For more detail see JLS for String literals..
 
Gireesh Giri
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 375
1
Java Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jain it helps me a lot :) .
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic