How do you compare Floats to Floats (wrapper classes)? Some books I've read say always use .equal'. One says that in comparing Floats, "= =" is the same as '.equal'.
Do they both give the same answer? If not, which one gives teh result you want when comparing two Floats with the same wrapped value?
"I'm not back." - Bill Harding, Twister
Zakaria Haque
Ranch Hand
Joined: Jan 02, 2002
Posts: 60
posted
0
Originally posted by liz riley: How do you compare Floats to Floats (wrapper classes)? Some books I've read say always use .equal'. One says that in comparing Floats, "= =" is the same as '.equal'.
According to javadoc api spec "Note that in most cases, for two instances of class Float, f1 and f2, the value of f1.equal.(f2) is true if and only if f1.floatValue() == f2.floatValue()"
tobe bondhu nouka bherao<br />shonabo gaan aj shara raat
Sayed Ibrahim Hashimi
Ranch Hand
Joined: May 17, 2001
Posts: 148
posted
0
When comparing Objects you want to use the .equals(..) method. Because when you do == that is checking to see if the references are the same not wether they are storing the same values. Hope this helps.
When comparing Objects you want to use the .equals(..) method. Because when you do == that is checking to see if the references are the same not wether they are storing the same values.
If you are referring to f1==f2 that is certainly correct, but the Float wrapper class has a method called floatValue() which returns the corresponding primitive of a Float, in which case, f1.floatValue()==f2.floatValue() is also appropriate for comparing Floats. -anthony
Thomas Smets
Ranch Hand
Joined: Dec 11, 2001
Posts: 111
posted
0
Originally posted by Anthony Villanueva:
If you are referring to f1==f2 that is certainly correct, but the Float wrapper class has a method called floatValue() which returns the corresponding primitive of a Float, in which case, f1.floatValue()==f2.floatValue() is also appropriate for comparing Floats. -anthony
Which is probably what the equals () does ! Thomas,