This week's book giveaway is in the Android forum.
We're giving away four copies of Head First Android and have David & Dawn Griffiths on-line!
See this thread for details.
Win a copy of Head First Android this week in the Android forum!
  • 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
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

equals on array

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, My question from Inquisition (SCJP5 quiz by Mark Dechamps).


The answer is false. Can anyone explain?

When I try,


Many thanks,

Edmen.
 
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Edmen Tay
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Edmen, I think you got it!
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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)
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To compare int[] you can use the equals function in the Arrays class.
, int[])]Arrays
 
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Edmen, did you try this one ?

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

 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ya this restriction is for == operator, I forgot the case, actually I had raised this question for == operator.
 
Edmen Tay
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic