Question

I would expect the byte representation of two identical strings to also be identical yet this does not seem to be the case. Below is the code I've used to test this.

    String test1 = "125";
    String test2 = test1;

    if(test1.equals(test2))
    {
        System.out.println("These strings are the same");
    }

    byte[] array1 = test1.getBytes();
    byte[] array2 = test2.getBytes();

    if(array1.equals(array2))
    {
        System.out.println("These bytes are the same");
    }
    else
    {
        System.out.println("Bytes are not the same:\n" + array1 + " " + array2);
    }

Thanks in advance for your help!

Was it helpful?

Solution

Byte representations of two identical but unrelated String objects would most certainly be identical byte-for-byte. However, they would not be the same array object, as long as the String objects are unrelated.

Your code is checking array equality incorrectly. Here is how you can fix it:

if(Arrays.equals(array1, array2)) ...

Moreover, you would get different byte arrays even if you call getBytes on the same String object multiple times:

String test = "test";
byte[] a = test.getBytes();
byte[] b = test.getBytes();
if (a == b) {
    System.out.println("same");
} else {
    System.out.println("different");
}

The above code prints "different".

This is because the String does not persist the results of getBytes.

Note: your code does call getBytes on the same object twice, because this line

String test2 = test1;

does not copy the string, but creates a second reference to the same string object.

OTHER TIPS

The issue is that array1.equals(array2) is not doing what you think it does; it returns equivalent but not reference-identical arrays. If you used Arrays.equals(array1, array2), it would have worked.

getBytes() returns different value every time we call on any object so we will get different values when we call like below

System.out.println("1 test1.getBytes() "+test1.getBytes());
 System.out.println("2 test2.getBytes() "+test2.getBytes());

both the values are different and storing in a byte[] array1 and byte[] array2 respectively, since both are different bytes , 2 new byte array got created and hence their comparison with equals method compares the references and returns false.

so use Arrays.equals(array1, array2) this to compare the actual content in those 2 byte arrays.

Some more explanation about equals:

The equals method in Object is the same as ==; that is, it compares two references to see if they're equal.

However, equals is overridden in the String class, so that equals on two strings compares the contents of the strings, instead of the references. That's why you need to use equals when comparing Strings, instead of ==. Many other classes in the Java runtime also override equals, so that they do something other than comparing references.

Array classes (the "class" to which array objects belong), however, do not override equals. I think the main reason is that there's no named array class in which an overriding equals could appear. Thus, the idea of using equals to compare the contents, which works for Strings, doesn't work for arrays. The result is that, for an array x, x.equals(y) is the same as x == y (if x isn't null). It uses the inherited equals from Object.

That's why the Arrays class is provided: to give you some of the methods that can't be provided because there's no class to put them in. Arrays.equals and Arrays.toString are two such methods that you'd use instead of x.equals or x.toString.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top