Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question on similar & identical of Objects

 
xiao sean
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20667
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because array equality does not check its contents. Use java.util.Arrays.equals(array1, array2) instead to check for content equality as well.
 
Tony Docherty
Bartender
Posts: 2989
59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20667
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[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 ]
 
xiao sean
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2989
59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for the advice. i manage to found the above mention ways.

thanks.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic