K
kvnsmnsn
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.");
}
}
}
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.");
}
}
}