• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Question on similar & identical of Objects

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for the advice. i manage to found the above mention ways.

thanks.

 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic