• 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
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Abstract

 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First let me have the honor to Congratulate Jim, Tony, Maha and all responsible for this site. I call it the "Site No.1"...
Question:
the equals method in the object is
equals(object);
can we do
Integer a = new Integer(5);
Float f = new Float(5);
will the result of
a.equals(f)
be
1) true
2) false
3) compiler error
4) exception
could i get an expanation ???
is it that equals always gets the toString function called for the object...
(please dont mind this dump question.. just a bit confused about the equals method)


------------------
denice the menace
 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The answer in this particular case will be false. This is because the Integer wrapper class overrides the Object version of equals so that it returns true only if the argument is an Integer object that contains the same int value as the compared object. Several other classes similarly override the Object version of equals to provide a specific, more useful equals method.
The Object version of equals effectively checks to see if the compared objects are the same object - it's pretty much the same as using the == operator.
 
Desperado
Posts: 3226
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As Lucy said, the objects have to belong to the same class besides also having the same numeric value for the equals() to return true for the classes in your example.
[This message has been edited by Tony Alicea (edited March 10, 2000).]
 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you right, Lucy
to be more precise:
"equals()" tests whether two distinct objects have byte-for-byte equivalence
"==" tests whether two variables refer to the same object
 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
jane, not strictly byte-to-byte...depends on how the class in question wants to implement equals(Object).
One nasty little detail ( )to remember is that while String has it's own equals(), StringBuffer is left with the raggedly old one it inherited from Object, with the result that:

StringBuffer s1=new StringBuffer("hello");
StringBuffer s1=new StringBuffer("hello");
System.out.println(s1.equals(s2)?"yes":"no");

returns, you guessed it!
 
Rolf Weasel
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
er...that code should read:
StringBuffer s1=new StringBuffer("hello");
StringBuffer s2=new StringBuffer("hello");
System.out.println(s1.equals(s2)?"yes":"no");
 
I'm so happy! And I wish to make this tiny ad happy too:
Clean our rivers and oceans from home
https://www.kickstarter.com/projects/paulwheaton/willow-feeders
reply
    Bookmark Topic Watch Topic
  • New Topic