Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

q from Sreenivas Majji

 
JayaSiji Gopal
Ranch Hand
Posts: 303
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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".

Why does this code return false? plz explain.
 
sanjeevmehra mehra
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if(b1==b1) //true, comparing same object
if(b1.equals(b1)) //true, same String value returned by toString()


if(b1.toString()==b1.toString())//flase,
b1="hi";
b2="h1";
if(b1==b2)//false, comparing two different objects
if(b1.toString()(anonymous object one) == b1.toString()(anonymous object two))//flase, comparing two different objects.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic