Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > default equals function?

Reply
Thread Tools

default equals function?

 
 
bob smith
Guest
Posts: n/a
 
      08-09-2012
Let's say I have a class like this:

public class Kern_Pair {
int letter1, letter2;

}

Can someone tell me how the default equals function will behave?

like

kernpair1.equals(kernpair2)

?
 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      08-09-2012
On 8/9/2012 5:58 PM, bob smith wrote:
> Let's say I have a class like this:
>
> public class Kern_Pair {
> int letter1, letter2;
>
> }
>
> Can someone tell me how the default equals function will behave?
>
> like
>
> kernpair1.equals(kernpair2)
>
> ?


Teachable Moment.

The Kern_Pair class has no equals() method of its own, so it
inherits equals() from the nearest superclass that has one. Walk
up the class inheritance tree to find the nearest ancestor with an
equals(). The class inheritance tree for Kern_Pair is fairly short:
Its immediate superclass is Object (because you didn't say "extends
SomethingElse"), so the equals() method for Kern_Pair *is* the
equals() method of Object.

Now all you need to do is go to the Javadoc and study what
Object's equals() will do.

--
Eric Sosman
d
 
Reply With Quote
 
 
 
 
Jeff Higgins
Guest
Posts: n/a
 
      08-09-2012
On 08/09/2012 05:58 PM, bob smith wrote:
> Let's say I have a class like this:
>
> public class Kern_Pair {
> int letter1, letter2;
>
> }
>
> Can someone tell me how the default equals function will behave?
>
> like
>
> kernpair1.equals(kernpair2)
>
> ?

<http://docs.oracle.com/javase/tutorial/java/IandI/objectclass.html>
Here is a convenient dashboard to the Oracle Java documentation.
<http://docs.oracle.com/javase/7/docs/>
You may replace the 7 in the above address with a 3, 4, 5 or 6
to see older versions documentation.
 
Reply With Quote
 
bob smith
Guest
Posts: n/a
 
      08-10-2012
On Thursday, August 9, 2012 5:11:07 PM UTC-5, Eric Sosman wrote:
> On 8/9/2012 5:58 PM, bob smith wrote:
>
> > Let's say I have a class like this:

>
> >

>
> > public class Kern_Pair {

>
> > int letter1, letter2;

>
> >

>
> > }

>
> >

>
> > Can someone tell me how the default equals function will behave?

>
> >

>
> > like

>
> >

>
> > kernpair1.equals(kernpair2)

>
> >

>
> > ?

>
>
>
> Teachable Moment.
>
>
>
> The Kern_Pair class has no equals() method of its own, so it
>
> inherits equals() from the nearest superclass that has one. Walk
>
> up the class inheritance tree to find the nearest ancestor with an
>
> equals(). The class inheritance tree for Kern_Pair is fairly short:
>
> Its immediate superclass is Object (because you didn't say "extends
>
> SomethingElse"), so the equals() method for Kern_Pair *is* the
>
> equals() method of Object.
>
>
>
> Now all you need to do is go to the Javadoc and study what
>
> Object's equals() will do.
>
>
>
> --
>
> Eric Sosman
>
> d


The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any non-null reference values x and y, this method returns true if and only if x and y refer to the same object (x == y has the value true).
 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      08-10-2012
On 8/10/2012 10:12 AM, bob smith wrote:
> On Thursday, August 9, 2012 5:11:07 PM UTC-5, Eric Sosman wrote:
>> On 8/9/2012 5:58 PM, bob smith wrote:
>>> [...]
>>> Can someone tell me how the default equals function will behave?

>> [...]
>> Now all you need to do is go to the Javadoc and study what
>> Object's equals() will do.

>
> The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any non-null reference values x and y, this method returns true if and only if x and y refer to the same object (x == y has the value true).


... and there you have it. Any questions?

--
Eric Sosman
d
 
Reply With Quote
 
Arne Vajhøj
Guest
Posts: n/a
 
      08-10-2012
On 8/9/2012 6:22 PM, Jeff Higgins wrote:
> Here is a convenient dashboard to the Oracle Java documentation.
> <http://docs.oracle.com/javase/7/docs/>
> You may replace the 7 in the above address with a 3, 4, 5 or 6
> to see older versions documentation.


And surprising that works (via redirect).

Surprisingly because Java SE 3 and 4 are J2SE 1.3 and 1.4!

Someone at Oracle has been smart.

Arne

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
default equals function? bob smith Java 2 08-10-2012 07:41 PM
Re: default equals function? Eric Sosman Java 3 08-10-2012 06:39 PM
different asp.net server tags (less than Percentage symbol hash or equals) fake ID ASP .Net 1 09-29-2009 08:13 PM
does String.equals breaks the equals contract? Edward A Thompson Java 4 02-11-2004 11:33 PM
Difference? myDate.Equals(Date.MinValue) -or- myDate = Date.MinValue Andreas Klemt ASP .Net 3 08-10-2003 03:43 AM



Advertisments