Array.equal() giving wrong output

To my understanding, the following code should print true, since both elements are equal.

From java docs Array.get() will return:

Returns the value of the indexed component in the specified array object. The value is automatically wrapped in an object if it has a primitive type.

However, when I run the following code it is printing false:

  public class Test1 {

    static boolean equalTest(Object array1, Object array2) {
        return Array.get(array1, 0).equals(Array.get(array2, 0));
    }

    public static void main(String[] args) {
        int[] a = new int[1];
        byte[] b = new byte[1];
        a[0] = 3;
        b[0] = 3;
        System.out.println(equalTest(a, b));
    }
}

My question is isn't classes implementing Number are or should be directly comparable to one another.

Jon Skeet
people
quotationmark

This has nothing to do with arrays really. Your comparison is equivalent to:

Object x = Integer.valueOf(3);
Object y = Byte.valueOf((byte) 3);
boolean equal = x.equals(y);

That's never going to return true.

Even though your original arrays are of the primitive types, Array.get returns Object, so you're getting the boxed types - and comparing values of those different types.

people

See more on this question at Stackoverflow