aspose file tools*
The moose likes Beginning Java and the fly likes Question on similar & identical of Objects Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Question on similar & identical of Objects" Watch "Question on similar & identical of Objects" New topic
Author

Question on similar & identical of Objects

xiao sean
Ranch Hand

Joined: Aug 20, 2007
Posts: 44
hi, guys

i need some help on my program codes.
i don't understand why my array1 & 3 is not similar and since my array2 is point to array1, why my array2 is not similar to 3 either?

or isit something wrong with my program?



RESULT
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19541
    
  16

Because array equality does not check its contents. Use java.util.Arrays.equals(array1, array2) instead to check for content equality as well.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 1936
    
  28
Testing for equality is something that seems to cause no end of problems for Java novices so don't worry you're not alone in not grasping this.

The important thing to remember is that when using == with objects it tests whether the two variables are referencing the same object and not whether the objects contain the same information. If you want to test to see if 2 different objects are the same then you have to use the equals() method.
xiao sean
Ranch Hand

Joined: Aug 20, 2007
Posts: 44
Originally posted by Tony Docherty:
Testing for equality is something that seems to cause no end of problems for Java novices so don't worry you're not alone in not grasping this.

The important thing to remember is that when using == with objects it tests whether the two variables are referencing the same object and not whether the objects contain the same information. If you want to test to see if 2 different objects are the same then you have to use the equals() method.





hi, thanks for your reply. very much help in understanding certain part of equality.
however, even after i applies equals(). the end result is still same.
array1 & 3 is not similar.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19541
    
  16

That's because sometimes classes don't have a sophisticated equals implementation.

For instance, the equals method of Object just returns whether the two references point to the same object:

Programmers have to override the equals method to returns something more useful, like testing if the name or some other property is the same, or even multiple properties.

It seems however, that arrays just inherit Object's equals method, thereby checking only using ==. Like I said before, java.util.Arrays.equals(array1, array2) will check the entire contents of both arrays, doing what you need.

Well, until you pass an array of arrays, since the equals(Object[], Object[]) method uses equals for all elements of the arrays - if these are arrays themselves you still wind up with the same problem.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[Rob']: Well, until you pass an array of arrays, since the equals(Object[], Object[]) method uses equals for all elements of the arrays - if these are arrays themselves you still wind up with the same problem.

This is addressed by Arrays.deepEquals(), available in JDK 5 and later.
[ October 15, 2007: Message edited by: Jim Yingst ]

"I'm not back." - Bill Harding, Twister
xiao sean
Ranch Hand

Joined: Aug 20, 2007
Posts: 44
hi, sorry i will like to check how to i implement



into the program to check the equals of array1 & 3 whether are they similar
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 1936
    
  28
You don't need to implement this method it's already provided by the java.util.Arrays class. But why do you want to anyway, as Rob has said twice now, just use the java.util.Arrays.equals(..) method. Your arrays are one dimensional primitive type arrays and the Arrays.equals(..) method will work perfectly well.
xiao sean
Ranch Hand

Joined: Aug 20, 2007
Posts: 44
thanks for the advice. i manage to found the above mention ways.

thanks.

 
jQuery in Action, 2nd edition
 
subject: Question on similar & identical of Objects
 
Similar Threads
please can you help to identify those errors
array of arrays
method takes longer each time
convert ArrayList of String[] to String[][]
question concering hashCode(), equals() and operator ==