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

Discussion in 'Java' started by Ong Hong Peow, Sep 6, 2004.

  1. 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.
     
    Ong Hong Peow, Sep 6, 2004
    #1
    1. Advertising

  2. Ong Hong Peow

    David Hilsee Guest

    "Ong Hong Peow" <> wrote in message
    news:chgmvj$sfi$...
    > 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.

    --
    David Hilsee
     
    David Hilsee, Sep 6, 2004
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Edward A Thompson
    Replies:
    4
    Views:
    549
    Tony Morris
    Feb 11, 2004
  2. Oliver Wong
    Replies:
    14
    Views:
    1,671
    Chris Uppal
    Jun 13, 2006
  3. Larry Bud
    Replies:
    1
    Views:
    411
    Kevin Spencer
    Jun 28, 2007
  4. Sebastian Paul
    Replies:
    3
    Views:
    414
    Sebastian Paul
    Aug 17, 2007
  5. Ilias Lazaridis
    Replies:
    24
    Views:
    345
    Josef 'Jupp' SCHUGT
    May 12, 2005
Loading...

Share This Page