aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes equals on array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "equals on array" Watch "equals on array" New topic
Author

equals on array

Edmen Tay
Ranch Hand

Joined: Oct 21, 2008
Posts: 39
Hi, My question from Inquisition (SCJP5 quiz by Mark Dechamps).


The answer is false. Can anyone explain?

When I try,


Many thanks,

Edmen.
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
It seems equals is not overridden for arrays, which means that equals in the case of arrays will be testing for object identity. However, wrappers do override equals, and two instances of a wrapper class are equal if they are both instances of the same class, and if the values they hold are equal.


All code in my posts, unless a source is explicitly mentioned, is my own.
Edmen Tay
Ranch Hand

Joined: Oct 21, 2008
Posts: 39
Alright, I think i got what you trying to meant.

for array, it is not overriden and it will run,

for wrapper class, it will be overriden and will run,


Please correct if i am wrong.

Many thanks.

Edmen.
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
Edmen, I think you got it!
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
I forgot to say that there are a couple of methods in the java.util.Arrays class that do what you want:

static boolean equals(Object[], Object[])
and
static boolean equals(primitive[], primitive[])

(K&B, p. 593)
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

To compare int[] you can use the equals function in the Arrays class.
, int[])]Arrays


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
Edmen, did you try this one ?

Integer c = new Integer(128);
Integer d = new Integer(128);
System.out.println(c.equals(d));


SCJP 6
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
Punit, are you thinking of the equality (==) operator? That's the one that has the restriction for the Integer type of -128 to 127. This case would be no different for equals than the other case.

I forgot to mention that for the == operator to report equal on the Integer objects it is also necessary that they have been created through autoboxing, and not via the new() operator. The new operator will always create a new object on the heap (similar to what happens with Strings and the constant pool.)
[ December 29, 2008: Message edited by: Ruben Soto ]
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
Ya this restriction is for == operator, I forgot the case, actually I had raised this question for == operator.
Edmen Tay
Ranch Hand

Joined: Oct 21, 2008
Posts: 39
Hi Punit,

Integer c = new Integer(128);
Integer d = new Integer(128);
System.out.println(c.equals(d));


It is true.

Integer MAX VALUE is 2147483647 and MIN VALUE is -2147483648

Edmen
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: equals on array