Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes QUESSION OF equls() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "QUESSION OF equls()" Watch "QUESSION OF equls()" New topic


Hades Pan
Ranch Hand

Joined: Nov 25, 2001
Posts: 106
public static void main(String args[])
Float f=new Float(4.2f);
Float c;
Double d=new Double(4.2);
float fl=4.2f;
which will return true?. Select all
Ans is c==f and c.equls(f)
But why equls is right too?
Rashmi Tambe
Ranch Hand

Joined: Aug 07, 2001
Posts: 418
Hi Hades,
== checks if both the references c and are pointing to same object. In this case, yes.
equalsjust checks if both references are refering to same value. Now and f are pointing to same object so equals would return true as they both contain value 4.2f. In other word...
equals would always return true if == returns true. vice a versa is not always true
correct me if i am wrong.
Hades Pan
Ranch Hand

Joined: Nov 25, 2001
Posts: 106
Thank Rashmi very much~~!
I agree. Here's the link:
subject: QUESSION OF equls()
It's not a secret anymore!