aspose file tools*
The moose likes Beginning Java and the fly likes cloning, equals() - not understanding the behaviour Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "cloning, equals() - not understanding the behaviour" Watch "cloning, equals() - not understanding the behaviour" New topic
Author

cloning, equals() - not understanding the behaviour

Jan Groth
Ranch Hand

Joined: Feb 03, 2004
Posts: 456
good evening from germany,

i have a situation like this:

a data class:


a test class:


the test fails at the last line.

okay, first guess, easy: Data.readRecord(1) returns something else, that's the failure. but check this:

this is the output of my runtime debugger:



my question:
if the two string arrays are equal on a line-by-line base, and have the same length, why does the direct equal() return true?

and idea appreciated,

thank you very much,

Jan
Jan Groth
Ranch Hand

Joined: Feb 03, 2004
Posts: 456
stupid me, the question is:

if the two string arrays are equal on a line-by-line base, and have the same length, why does the direct equal() return false?

******

i would really appreciate a preview functionality in this forum...
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

Because arrays don't override the "equals" method. Interestingly enough, someone just raised this very question over in the Java in General (Advanced) forum; see that thread for a more detailed answer.

There's a family of overloaded two-argument static methods named java.util.Arrays.equals() which you can use to compare arrays elementwise.


[Jess in Action][AskingGoodQuestions]
Jan Groth
Ranch Hand

Joined: Feb 03, 2004
Posts: 456
perfect answer, thanks a lot!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: cloning, equals() - not understanding the behaviour
 
Similar Threads
String and StringBuffer equals method
Should I synchronize on data in bookHotel method?
design question: should the lock manager be synchronized whenever it's used in Data?
the famous ; error revisited
JUnit: How to test read/write file