class s11 { public static void main(String[] args) {
String s11 = new String("H"); String s12 = s11;
System.out.println(s11==s12); } }
the output is true. shldnt it be false. s11 creates a new string object with new. and s12 is assigned s11. but s12 is in the string pool and s11 is a new string so how does this result true using ==. pls clear this thx regards Kamal
hi , h is the only object in memory whose reference has been assigned in two direction's for eg ss2-------->ss1--------->"h"
ss2 is pointing the same object and ss1 is also pointing the same object.They have same reference's in our case. rememeber == compares the reference's if they are same it give's true otherwise false
Hi kamal jaisingh, When u create a String with a new operator u actaually create a String object. That is your String s11 = new String("H"); Here s11 is object reference which holds a pointer to H. Now in String s12 = s11; we have assigned the object reference s11 to s12. In other words after this operation both s11 and s12 will point to "H". Now equality operator == will be checking for the object reference equality and in this case it is.. Hence the answer. Correct me if I am wrong.