• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

equal()

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Double d=new Double(0.9);
Float f= new Float(0.9f);
why is f.equals(d) returns false.
I know that equals()test for value.
if double d=0.9;
float f=O.9f;
d==f returns true.
why when the value of object is checked it returns false.
I think I am missing some thing.Please point it out.
-Thanks
 
Ranch Hand
Posts: 262
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In this case, equals() returns false because you are comparing objects of two different types.
Many implementations of the ClassType.equals(object x) method say something like:
"Only return true if and only iff x is instance of ClassType and . . . "
Remember you're not using equals() to compare th values wrapped by the wrapper classes. You are comparing the objects.
 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI Tony,
In ur code:
Double d=new Double(0.9);
Float f= new Float(0.9f);
d and f are two different types of objects one is of type Double and other Float and hence the tyoe of value they represent is different.Thats why
f.equals(d) returns false ..
Now secondly,
double d=0.9;
float f=O.9f;
d==f returns true.
for == operator the binary numeric promotion is applied and so when we compare double and float ..the value
of float (0.9f) gets promoted to double ..
so u get the answer as true.
This type of automatic conversion is not applicable to Object types (here it is Double and Float).
I hope u get it.
Swapna
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
double d=0.9;
float f=0.9f;
System.out.println("d==f " + (d==f));
if(d==f)
System.out.println("d==f=true");
I tried this , but got answer d==f as false
 
Ranch Hand
Posts: 295
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If d had been initialized as
double d = 0.9f;
then it would have printed true.

Clement
 
Ana P
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
double d21=10.3;
float f21=10.3f;
System.out.println("d==f " + (f21==d21));
if(f21==d21)
System.out.println("d==f=true");
This does not return true for all values
like 0.9. 0.5
I think while promotion it loses precision.
am i right?
 
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Ana P"-
Welcome to the JavaRanch! Please adjust your displayed named to match the JavaRanch Naming Policy. You can adjust it here.
Thanks! and welcome to the JavaRanch!
 
Paddy spent all of his days in the O'Furniture back yard with this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic