Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Comparing byte arrays

807597Jul 1 2004 — edited Jan 18 2005
Hi. I guess this is a rather easy task, but I've bumped.

I have a byte array that I've read from a file. It's a header of a file and does ALWAYS look like this: 0x52 0x61 0x72 0x21 0x1a 0x07 0x00. I have a predefined byte array which also looks the same (this is the one I'll check against). Now, if I try to compare these two arrays, they differ, even though they should be the same. If I print them out, they differ ([B@14f8dab and [B@1ddebc3), but if I turn them both into hex strings, they match (52 61 72 21 1a 07 00 which both strings are), even if I work through each element in the array and check, they match (82 97 114 33 26 7 0).

Why doesn't the basic byte array to byte array comparison work, and why does System.out.println() return different results, even though they contain the same values?
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Feb 15 2005
Added on Jul 1 2004
8 comments
382 views