• 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

Arrays.equals method produces different output.

 
Ranch Hand
Posts: 113
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
please explain why the output is "false" when i try to compare two array of my own class while output is true when i use array of String or array of Integer.
i am gettign following output
true
true
false(please explain this)
-------------------------------------------------------------
[edit]add code tags. cR[/edit]
[ October 06, 2008: Message edited by: Campbell Ritchie ]
 
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
have a look at the sun java docs for the Arrays.equals methods and you should find the answer.

Hint: have you overridden equals in your Testing123 class?
 
Tanu Gulati
Ranch Hand
Posts: 113
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sun java doc for the method in Arrays says "the two arrays are equal if they contain the same elements in the same order. Also, two array references are considered equal if both are null"

i have copied the whole program and as you can see i have not overriden the equals method.
anyway thanks for posting

Tanu Gulati
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you had read the whole of the description of the method in the Arrays class you would know that Paul Beckett is giving you a useful hint which you appear to be ignoring.
 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Another hint:


Do you know what's happening and why? Have another look at Paul Beckett's post!
 
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Two objects e1 and e2 are considered equal if (e1==null ? e2==null : e1.equals(e2)).
 
Tanu Gulati
Ranch Hand
Posts: 113
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think there is some miscommunication please look the disussion thread from the begining. it is not two objects i am talking about it is the two array of objects we are discussing and as per java doc
"The two arrays are considered equal if both arrays contain the same number of elements, and all corresponding pairs of elements in the two arrays are equal."

Are not the two arrays contain same number of element in similar way as we have for String and Integer objects in the Program?? please look carefully.
thanks,
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


"The two arrays are considered equal if both arrays contain the same number of elements, and all corresponding pairs of elements in the two arrays are equal." (emphasis added)



Your answer is in your question itself.
How do you think the JVM would figure out if the corresponding elements are equal?
 
Tanu Gulati
Ranch Hand
Posts: 113
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i got it!! thanks friends.
 
Tanu Gulati
Ranch Hand
Posts: 113
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i override equals method to find out put a condition when we call two objects of type Testing123 as equal...

thanks for all of your support. this was my very first post and i really liked the response from the community of java. Is there any way to close the current thread of discussion or it will be automatically closed by tommorow. let me know i have to close the disucssion on this particular topic.



Tanu Gulati
 
Maneesh Godbole
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are welcome
We are also happy to have you here.

No. You don't need to do anything to "close" the topic. Here at the ranch, the moderators usually "close" the topic; meaning the topic is no longer open for discussion.
 
Thomas Thevis
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

i override equals method to find out put a condition when we call two objects of type Testing123 as equal...


Don't want to be too pedantic, but if you override equals(), you should also osverride hashCode(). Have a look at the API docs for class java.lang.Object and especially the general contract of hashCode().
If you don't, other classes might not work as expected.

Regards,
Thomas
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually, topics don't get closed just because they are "solved", but only when they are in some way inappropriate for where they were posted.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic