• 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
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

equals()

 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is there a specific list of classes which override the equals() method for "deep content comparision"?
help me get the names of those classes.
thanks
zarina
 
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Zarina,
I doubt that any such list exists, but the javadocs will tell you which classes have overridden the equals method. For example, the wrapper classes such as Boolean have overridden the equals method. A more interesting example of a deep comparison might be the ArrayList class.
 
zarina mohammad
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From Jxam mock exam

In the above code, (f1.equals(d1)) does not return true even though the wrapper class Float overrides the equals() method for content comparision. what could be the reason??
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Zarina,
The code that you posted prints true in all three cases, so it is difficult to determine what is going on. I modified the code as follows.

When I ran the code it printed f1.equals(f2). It appears to do the comparison correctly.
 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dan, Note that Zarina asked



In the above code, (f1.equals(d1)) does not return true even though the wrapper class Float overrides the equals() method for content comparision. what could be the reason??


In this case f1.equals(d1) doesn't return true and shouldn't return true! Remember 10F and 10D are not equal
 
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
These are the steps equals() methods in the wrapper classes go through:
[code]
Is the argument null (1)?
Yes: then return false.
No : Is the argument same type as myself (2)?
No: then return false.
Yes: Do we have the same value (3)?
No: then returns false.
Yes: returns true.
[code]
equals() returns false even if they hold the same
value but being of different types.
Questions involving the above understanding will
most likely appear in the real exam.
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Amir,
You are correct on both counts. First, I did indeed misread the question. I thought that Zarina was asking about the f1.equals(f2) case. Instead, Zarina was asking about f1.equals(d1).
You are also correct in saying that f1.equals(d1) is false. Two objects can not be equal if they are not of the same type.
 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yup, as the documentation says, the .equals
argument has to be a Float if it is to return
true on a Float object.
> 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.
--------------------------------------------
"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:
If f1 and f2 both represent Float.NaN, then the equals method returns true, even though Float.NaN==Float.NaN has the value false.
If f1 represents +0.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 definition allows hash tables to operate properly"
Considering the Certified Java Programmer Exam?
Get JCertify!
Expert language content, outstanding practice exams
Eckel, Baldwin, Green, and more
http://www.enterprisedeveloper.com/jcertify
 
Amir Ghahrai
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also note that if int x = 10 and float y = 10.0f then x == y will return true.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One more note to add, it appears there might be some confusion due to the fact that no promotion takes place in this example when doing the comparisons (such as f1.equals(d1))). No promotion occurs with the wrapper classes.
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Slim Jim,
Welcome to Javaranch
We'd like you to read the Javaranch Naming Policy and change your publicly displayed name to comply with our unique rule. Thank you.
 
Those who dance are thought mad by those who hear not the music. This tiny ad plays the bagpipes:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic