Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / April 2006

Tip: Looking for answers? Try searching our database.

Why Does <equals()> Method on Arrays Always Return False?

Thread view: 
kvnsmnsn@hotmail.com - 25 Apr 2006 00:03 GMT
I spent August 2004 to December 2005 as a teaching assistant to the
Intro to Computer Science class here at BYU, that used Java.  One
thing we've been very careful to tell the students is that if you want
to check for equality of two objects in Java, you use the <equals()>
method, not the "==" operator.  The <equals()> method is inherited by
every object from the <Object> class, so you can pretty much call it
on everything.

But I just found out that if you create an array of <int>s and popu-
late it with the very same values as you have in another array of
<int>s, and then call the <equals()> method on the two arrays, it re-
turns <false>, even though the two arrays are identical.  Anyone know
why this is doing this?  What use is the <equals()> method defined for
each array if I can't use it to tell whether two arrays are equal?

                               ---Kevin Simonson

"You'll never get to heaven, or even to LA,
if you don't believe there's a way."
from _Why Not_

public class EqualsCheck
{
 public static void main ( String[] arguments)
 {
   int[] original = new int[ 1];
   int[] copy     = new int[ 1];

   original[ 0] = 55;
   copy[ 0]     = 55;
   if (original.equals( copy))
   { System.out.println( "Original equals copy.");
   }
   else
   { System.out.println( "Original doesn't equal copy.");
   }
 }
}
Roedy Green - 25 Apr 2006 00:11 GMT
>But I just found out that if you create an array of <int>s and popu-
>late it with the very same values as you have in another array of
><int>s, and then call the <equals()> method on the two arrays, it re-
>turns <false>, even though the two arrays are identical.  Anyone know
>why this is doing this?  What use is the <equals()> method defined for
>each array if I can't use it to tell whether two arrays are equal?

Object.equals just acts like == and that is what array uses. You'd
think it makes so sense to have .equals defined as == for an array,
since if that is what you wanted you could always use ==.

However, your desired equals definition is much slower than ==.  ==
style equals would normally suffice for HashMap work. That is why they
probably defined array equals  that way.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Adam Maass - 25 Apr 2006 04:41 GMT
>I spent August 2004 to December 2005 as a teaching assistant to the
> Intro to Computer Science class here at BYU, that used Java.  One
[quoted text clipped - 10 lines]
> why this is doing this?  What use is the <equals()> method defined for
> each array if I can't use it to tell whether two arrays are equal?

Short answer: because the classes for arrays don't override the equals()
methods to do this.

Longer answer: probably because Sun didn't think about this. Furthermore,
today, java.util.Arrays.equals provides this functionality.
Oliver Wong - 25 Apr 2006 22:25 GMT
>> I just found out that if you create an array of <int>s and popu-
>> late it with the very same values as you have in another array of
>> <int>s, and then call the <equals()> method on the two arrays, it re-
>> turns <false>, even though the two arrays are identical.  Anyone know
>> why this is doing this?  What use is the <equals()> method defined for
>> each array if I can't use it to tell whether two arrays are equal?

[...]

> Longer answer: probably because Sun didn't think about this.

   To be fair, the semantics of "equality" is somewhat context dependent,
in that in some cases, for two arrays to be "equal", all that needs to be
true is that each of its elements are also "equal". In other cases, for two
arrays to be "equal", all that needs to be true is for its elements to be
identical (as in ==). In yet other cases, other semantics might apply.

   This is one reason why I wish there was something like the Comparator
interface for equality, so that you could use a different EqualityChecker
object for different semantics.

   It's somewhat "dirty" to use Comparator as an EuqualityChecker, and just
check about whether the compareTo() method returns == 0 or != 0, because
Comparator implies a total ordering over the instances of types, which might
not make sense for the data you're working with.

   - Oliver
Adam Maass - 26 Apr 2006 05:37 GMT
>    To be fair, the semantics of "equality" is somewhat context dependent,
> in that in some cases, for two arrays to be "equal", all that needs to be
[quoted text clipped - 10 lines]
> because Comparator implies a total ordering over the instances of types,
> which might not make sense for the data you're working with.

Long ago and far away, I did a significant amount of programming in Lisp.
Lisp has at least four different notions of equality:

eq
eql
equal
=

If I remember aright, eq tested that the two operands were bound to the same
object; eql evaluted to t if its operands were eq or if its operands were
lists whose elements were pairwise eq; equal evaluated to t if its operands
were eql or if its operands were lists that were pairwise eql.

Essentially, the longer the funtion name, the deeper the compare.

And = was for use with numeric quantities.
Tony Morris - 26 Apr 2006 22:53 GMT
>    This is one reason why I wish there was something like the Comparator
> interface for equality, so that you could use a different EqualityChecker
> object for different semantics.

There is. http://contractualj.com/api/net/tmorris/adt/Equalable.html

What you are eluding to, even if you don't know it, is that the OO paradigm
as we know it, is implicitly in contradiction to a formally stated
requirement specification. Think about it some more.

Signature

Tony Morris
http://tmorris.net/



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.