byte[] equals() only applies to same reference?

O

Ong Hong Peow

Hi,

I've assumed that byte[] does equals with the content too, it's otherwise.
e.g.

byte[] testA = new byte[4];
testA[0] = 0;
testA[1] = (byte)0xac;
testA[2] = (byte)0xab;
testA[3] = (byte)0xde;

byte[] testB = new byte[4];
testB[0] = 0;
testB[1] = (byte)0xac;
testB[2] = (byte)0xab;
testB[3] = (byte)0xde;

System.out.println( testB.equals( testA ) );

prints "false"

How do you compare byte[]?
memcpy() .. kinda equvilant? ...

Hopefully not writing my own.

Thanks.
 
D

David Hilsee

Ong Hong Peow said:
Hi,

I've assumed that byte[] does equals with the content too, it's otherwise.
e.g.

byte[] testA = new byte[4];
testA[0] = 0;
testA[1] = (byte)0xac;
testA[2] = (byte)0xab;
testA[3] = (byte)0xde;

byte[] testB = new byte[4];
testB[0] = 0;
testB[1] = (byte)0xac;
testB[2] = (byte)0xab;
testB[3] = (byte)0xde;

System.out.println( testB.equals( testA ) );

prints "false"

How do you compare byte[]?
memcpy() .. kinda equvilant? ...

Hopefully not writing my own.

Use java.util.Arrays.equals(). The java.util.Arrays class has all sorts of
methods that are useful when working with arrays.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top