wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Arrays.equals method produces different output. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Arrays.equals method produces different output." Watch "Arrays.equals method produces different output." New topic
Author

Arrays.equals method produces different output.

Tanu Gulati
Ranch Hand

Joined: Oct 06, 2008
Posts: 113

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 ]

thanks
Tanu Gulati
Paul Beckett
Ranch Hand

Joined: Jun 14, 2008
Posts: 96
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

Joined: Oct 06, 2008
Posts: 113

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

Joined: Oct 13, 2005
Posts: 38441
    
  23
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

Joined: Sep 02, 2008
Posts: 87
Another hint:


Do you know what's happening and why? Have another look at Paul Beckett's post!


SCJP 5.0, SCJD in progress
Srikanth Basa
Ranch Hand

Joined: Jun 06, 2005
Posts: 241

Two objects e1 and e2 are considered equal if (e1==null ? e2==null : e1.equals(e2)).
Tanu Gulati
Ranch Hand

Joined: Oct 06, 2008
Posts: 113

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

Joined: Jul 26, 2007
Posts: 10268
    
    8


"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?


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Tanu Gulati
Ranch Hand

Joined: Oct 06, 2008
Posts: 113

i got it!! thanks friends.
Tanu Gulati
Ranch Hand

Joined: Oct 06, 2008
Posts: 113

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

Joined: Jul 26, 2007
Posts: 10268
    
    8

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

Joined: Sep 02, 2008
Posts: 87
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

Joined: Jul 11, 2001
Posts: 14112
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.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Arrays.equals method produces different output.