Originally posted by Chris Ben:
Thanks. Great explanation. So for
c=a+b; even if the literal result of the a+b is same as a string literal in the String pool, c will not take(refer) it but still make a duplicated new object anyway, right?
Originally posted by Chris Ben:
Here are the codes and output. Can anyone explain to me what is going on?
Thanks
String s1="ab";
String s2="abcd";
String s3="cd";
String s4=s1+s3;
s1=s4;
String s5="abcd";
System.out.println("s1"+((s1==s2)?"==":"!=")+"s2");//s1!=s2
System.out.println("s2"+((s4==s2)?"==":"!=")+"s4");//s4!=s2
System.out.println("s1"+((s1==s5)?"==":"!=")+"s5");//s2!=s5
System.out.println("s2"+((s2==s5)?"==":"!=")+"s5");//s2==s5
System.out.print("s4"+((s4==s5)?"==":"!=")+"s5");//s4!=s5
shashank
Originally posted by Chris Ben:
Thanks. Great explanation. So for
c=a+b; even if the literal result of the a+b is same as a string literal in the String pool, c will not take(refer) it but still make a duplicated new object anyway, right?
<A HREF="http://www.tipsmart.com" TARGET=_blank rel="nofollow">www.tipsmart.com</A>
<A HREF="http://www.tipsmart.com" TARGET=_blank rel="nofollow">www.tipsmart.com</A>
<A HREF="http://www.tipsmart.com" TARGET=_blank rel="nofollow">www.tipsmart.com</A>