简体   繁体   中英

Comparing two Strings using .equals() returns False, but their byte arrays are equal

I'm having some troubles when trying to send an image from a client to a server, because original image is different to the recieved one. In order to find the problem, i'm reading line by line of both image looking for the difference. When I compare the Strings line by line, for some lines using String#equals (eg lineo.equals(lined)) result is false, but as they seemed to be the same when I print them in the console, I also compare their byte arrays. Surprisingly, using Array.equals(lineo.getBytes(), lined.getBytes()) result is true. Both client and server are in the same computer.

Please help me to understand

  1. Where can I find the difference between both Strings
  2. Why both methods to compare, returns different results

     private void compareImages() throws IOException { File dest = new File("C:\\\\TempFiles\\\\" + fileName); File orig = new File("C:\\\\Users\\\\Andres\\\\Desktop\\\\B&N\\\\" + fileName); BufferedReader bro = new BufferedReader(new FileReader(orig)); BufferedReader brd = new BufferedReader(new FileReader(dest)); String lineo = bro.readLine(); String lined = brd.readLine(); System.out.println("Ready to read"); while (lineo!= null && lined!= null) { if(!lined.equals(lineo)) { System.out.println("lineo"); System.out.println(lineo); System.out.println("lined"); System.out.println(lined); System.out.println("arrayo"); System.out.println(printArray(lineo.getBytes())); System.out.println("arrayd"); System.out.println(printArray(lined.getBytes())); System.out.println("Are: " + Arrays.equals(lined.getBytes(), lineo.getBytes())); break; } lineo = bro.readLine(); lined = brd.readLine(); } bro.close(); brd.close(); } public String strArray(byte[] array){ String toRet = ""; for (byte b : array) { toRet += b; } return toRet; } 

    The result of the console was:

lineo

ÿÄ µ }!AQa"q2?'¡#B±ÁRÑð$3br‚

lined

ÿÄ µ }!AQa"q2?'¡#B±ÁRÑð$3br‚

arrayo

11-1-600-751602133243554400112512304175183349656198197734113205063-111-9583566-79-632182-47-16365198114-1269

arrayd

11-1-600-751602133243554400112512304175183349656198197734113205063-111-9583566-79-632182-47-16365198114-1269

Are: true

Please, have in mind that I could not copy some characters from the output.

Regards,

Andrés

Unequal strings do not have to produce different arrays when you do getBytes() .

The result depends on the platform's default charset, but when I run the following code

String str1 = "?";
byte[] arr1 = str1.getBytes();
String str2 = "\u0080";
byte[] arr2 = str2.getBytes();
System.out.println(str1.equals(str2));
System.out.println(Arrays.equals(arr1, arr2));

the output I see is

false
true

I don't know exactly what is going on here, but it looks like certain control characters get treated as '?' .

The correct way to understand why strings are different is to compare the character arrays returned by toCharArray() .

Comparing images using strings might not be the best way to about it. Compare their byte (Use ByteArrayInputStream).

The strings' return character could be different or there might be some encoding differences between them.

Strings are funny things. Strings are immutable. If you create two strings with the same value they both point to the same reference in memory. This is called interning. If you update one of those strings we get a new value in memory and the pointer of your variable points to the new value.

When you create your two strings you're assigning different values to them, it doesn't care if one is encoded and the other isn't, it doesn't actually know the difference at this point. You therefore have two string variables pointing to different values. When you do a .equals() you're checking the equivalency of the two string objects (do they point to the same thing; no they do not; therefore false).

Here's a good article from Microsoft that explains it better than I can.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM