Why is the answer false here..Can somebody explain. The following code will give 1: Byte b1 = new Byte("127"); 2: 3: if(b1.toString() == b1.toString()) 4: System.out.println("True"); 5: else 6: System.out.println("False"); A) Compilation error, toString() is not avialable for Byte. B) Prints "True". C) Prints "False".
some one,please correct me if i am wrong b1.toString() returns a new String. == will test whether they both have same memory address. this is only possible if it is only one object,referenced by two variables. here two new Strings created have different addresses. hence false returned
Hi Shyamsundar, I checked your link. String s = "a"; String ss = s.toString(); System.out.println(s == ss);
these return true -> fine
but try this, String s1 = new String("a"); String ss1 = s.toString(); System.out.println(s1 == ss1); it returns false, so (what i have concluded - correct me if wrong) when a object is created using new,and then toString is called another object(String) is returned. But String literals are an exceptions. What do u say??
cool! You seem to have a valid argument here. Maybe Jim Yingst can provide us with a definitive answer because he gave me an expanation as to how the toString()works (you saw that in the link I provided). But then, the new() operator was not involved in my question...Hmmm! Interesting. Shyam [This message has been edited by Shyamsundar Gururaj (edited September 11, 2001).]