Hi, Here is a question about String objects.. The statement ... String s = "Hello" + "Java"; yields the same value for s as ... String s = "Hello"; String s2= "Java"; s.concat( s2 ); 1.True 2.False The answer given is False. I think it should be true. Because although String objects are immutable, the final object pointed to by reference s is the same --> HelloJava. I tried running this and it says false (s.equals(new s)). How come? Where am I going wrong? Regards, Kapil
Hope this helps. Correct me if I am wrong.<p>Cheers <img src="smile.gif" border="0"> ,<br />Kapil
hi kapil when u say s="Hello"+"Java"; the "HelloJava" is assogned to s; where as s.concat(s2) return a concated new String ,which should be assigned to some String type var. here concatitation is done what value is not assigned to anything. if u say System.out.println(s.concat( s2 ))then it will show HelloJava right
The result of concatenation is a new string object say String s1 = "Hello"+"World" String s= "Hello" ; String s2 = "World" ; s.concat(s2); ** After this the value of s remains "Hello' only. so s1.equals(s) returns false.
However s1.equals(s.concat(s2) returns true. Hope it helps Ravi
Yup, yup, yup. Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop