• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Arrays.equals method produces different output.

 
Tanu Gulati
Ranch Hand
Posts: 113
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • 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 ]
 
Paul Beckett
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • 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 Java Spring
  • Mark post as helpful
  • send pies
  • 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
 
Campbell Ritchie
Sheriff
Pie
Posts: 50176
79
  • Mark post as helpful
  • send pies
  • 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.
 
Thomas Thevis
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another hint:


Do you know what's happening and why? Have another look at Paul Beckett's post!
 
Srikanth Basa
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
  • 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 Java Spring
  • Mark post as helpful
  • send pies
  • 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,
 
Maneesh Godbole
Saloon Keeper
Posts: 11173
14
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • 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 Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i got it!! thanks friends.
 
Tanu Gulati
Ranch Hand
Posts: 113
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • 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
Saloon Keeper
Posts: 11173
14
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic