Java: the same string returns different byte arrays

I expect that the byte representation of two identical strings will also be identical, but this does not seem to be the case. Below is the code I used to verify 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!

+6
source share
4 answers

The byte representations of two identical but unrelated String objects will undoubtedly be identical bytes for the byte. However, they will not be the same array object if String objects are not connected.

Your code incorrectly validates the array. Here's how you can fix it:

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

In addition, you will receive different byte arrays, even if you call getBytes on the same String object several 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 String does not save getBytes results.

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

 String test2 = test1; 

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

+5
source

The problem is that array1.equals(array2) does not do what you think it does; it returns equivalent but not referential identical arrays. If you used Arrays.equals(array1, array2) , this would work.

+5
source

getBytes () returns a different value every time we call an object, so we will get different values ​​when we call it below

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

both values ​​are different and stored in byte [] array1 and byte [] array2 respectively, since both are different bytes, 2 new byte arrays are created and, therefore, comparing them with the equals method compares the links and returns false.

so use Arrays.equals(array1, array2) to compare the actual contents in these double-byte arrays.

+1
source

A few more explanations about equals :

The equals method in Object same as == ; that is, he compares the two links to make sure they are equal.

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

Array classes (the class that array objects belong to), however, do not override equals . I think the main reason is that there is no named array array in which the equals override can be displayed. Thus, the idea of ​​using equals to compare content that works for String s does not work for arrays. As a result, for the array x , x.equals(y) will be the same as x == y (if x not null ). It uses the inherited equals from Object .

This is why the Arrays class is provided: to provide you with some of the methods that cannot be provided because there is no class to host them. Arrays.equals and Arrays.toString are two such methods that you would instead of x.equals or x.toString .

+1
source

All Articles