As far my understanding
String literals are created in a heap.
public class MAIN {
String s1 = "I am a string";
String s2 = s1;
String s3 = "I am a String";
String x1="abc";
String x2=x1;
String x3="abc";
public static void main(String[] args) {
MAIN m = new MAIN();
System.out.println("s1 == s2 : " + m.s1==m.s2);//1
System.out.println("s1 == s3 : " + m.s1==m.s3);//2
System.out.println("s2 == s3 : " + m.s2==m.s3);//3
System.out.println(m.x1==m.x2);//4
System.out.println(m.x1==m.x3);//5
System.out.println(m.x2==m.x3);//6
}
}
I think answer should be true for line 1 2 3 as it will point to same String value "I am String" .
But when i tried to compile it ,it gives false for line 1 2 3.
And if I make my String literal as final will they still point to same "I am String".
And I think that line 1 2 3 are exactly doing same as line 4 5 6 ,what is the difference ???
ANS:
line 1 2 3 --flase
line 4 5 6 --true
Please explain where I am going wrong?
[ January 22, 2007: Message edited by: Kasak Tahilramani ]