WHy the byte comparison doesn't work?

Posted on 2007-10-13
Last Modified: 2010-03-30
HI expert,

I have to byte array and they have the same content.  WHy the below codes

first16Byte[] = ...
second16Byte[] = ...
 boolean byteCompare = first16Byte.equals(second16Byte);
Why byteCompare always return false even thought the byte array content and size are identicial?
Question by:4eyesgirl
    LVL 7

    Accepted Solution

    That is not correct way of comparing two arrays.


    Arrays.equals(first16Byte, second16Byte);


    public static boolean equals(byte[] a,
                                 byte[] a2)

        Returns true if the two specified arrays of bytes are equal to one another. Two arrays are considered equal if both arrays contain the same number of elements, and all corresponding pairs of elements in the two arrays are equal. In other words, two arrays are equal if they contain the same elements in the same order. Also, two array references are considered equal if both are null.

            a - one array to be tested for equality.
            a2 - the other array to be tested for equality.
            true if the two arrays are equal.

    LVL 7

    Expert Comment


    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
    Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
    Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
    Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now