> Programming Languages > Java
Various Topics Home | Disclaimer | Report Adult Posts

Various Topics on Java



Java - "byte[] equals() only applies to same reference?" in Programming Languages


Old 09-06-2004   #1
..g .... ....
 
Default byte[] equals() only applies to same reference?

Hi,

I've ***umed 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.


 
Old 09-06-2004   #2
..v.. ..ls..
 
Default Re: byte[] equals() only applies to same reference?

"Ong Hong Peow" <iou@iwow.com.sg> wrote in message
news:chgmvj$sfi$1@mawar.singnet.com.sg...
> Hi,
>
> I've ***umed 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 cl*** has all sorts of
methods that are useful when working with arrays.

--
David Hilsee


 

Thread Tools
Display Modes





Powered by vBulletin®
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0