File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes q from Sreenivas Majji Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "q from Sreenivas Majji" Watch "q from Sreenivas Majji" New topic

q from Sreenivas Majji

JayaSiji Gopal
Ranch Hand

Joined: Sep 27, 2004
Posts: 303
The following code will give

1: Byte b1 = new Byte("127");
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.

SCJP 1.4, SCWCD 1.4<br /> <br />Thanks in advance!<br />Jayashree.
sanjeevmehra mehra
Ranch Hand

Joined: Aug 21, 2004
Posts: 75
if(b1==b1) //true, comparing same object
if(b1.equals(b1)) //true, same String value returned by toString()

if(b1==b2)//false, comparing two different objects
if(b1.toString()(anonymous object one) == b1.toString()(anonymous object two))//flase, comparing two different objects.

thanks & regards,<br />Sanjeev.
I agree. Here's the link:
subject: q from Sreenivas Majji
It's not a secret anymore!