• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

doubt: equals&==

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
given:
Float f2 = new Float("10F");
Double d1 = new Double("10D");
System.out.println(10F==10D); // return true
System.out.println(f2.equals(d1)); // return false
what the equals method of Float class do ? i do not understand.
can you tell me ?

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
equals method checks whether both points towards same location or not.
i am not sure whether i am right but still i am replying pls correct me if i am wrong
 
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


If the class overrides the equals() method of Object class
( i.e. the String, File, Date , BitSet and the wrapper classes )
then equals() will compare the contents and == is
for comparing the object reference.


Correct me if wrong
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
equals method takes in a object but will return true only if the input parameter is a float & has the same value as the float object whose equals method is being called i.e
Float f1 = new Float(10.0f);
Float f2 = new Float(10.0f);
Double d1 = new Double(10.0);
f1.equals(f2) returns true,
f1.equals(d1) returns false,
f1==f2 returns false as f1 & f2 point to different objects & == only checks if the two object references refer to same object & does not care to check the value stored int he object being referenced
------------------
Sun Certified Programmer for Java2 Platform scored 100%.
 
chetna_jain
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i am not clear with the difference between them
 
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Scott Xia:
given:
Float f2 = new Float("10F");
Double d1 = new Double("10D");
System.out.println(10F==10D); // return true
System.out.println(f2.equals(d1)); // return false
what the equals method of Float class do ? i do not understand.
can you tell me ?


Float != Double just like String != Float

equals() method of the Float class compares one Float with another Float to see if they are equal value. If you want to compare the Float value with the Double value, you need to extract the values.
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
from jdk api definition of Class Float:
public boolean equals(Object obj)
Compares this object against the specified object. The result is true if and only if the argument is not null and is a Float object that represents a float with the same value as the float represented by this object. For this purpose, two float values are considered to be the same if and only if the method floatToIntBits(float) returns the identical int value when applied to each.
Note that in most cases, for two instances of class Float, f1 and f2, the value of f1.equals(f2) is true if and only if
f1.floatValue() == f2.floatValue()

also has the value true. However, there are two exceptions:
1) If f1 and f2 both represent Float.NaN, then the equals method returns true, even though Float.NaN==Float.NaN has the value false.
2) If f1 represents .0f while f2 represents -0.0f, or vice versa, the equal test has the value false, even though 0.0f==-0.0f has the value true.

[This message has been edited by galen wang (edited September 21, 2001).]
 
Scott Xia
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you all
 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic