Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 (e.g. 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

like image 766
an3sarmiento Avatar asked Nov 12 '15 18:11

an3sarmiento


People also ask

When comparing two strings and we use equals () What are we comparing?

Java String equals() Method The equals() method compares two strings, and returns true if the strings are equal, and false if not.

What's the difference between equals () and == to compare string?

In simple words, == checks if both objects point to the same memory location whereas . equals() evaluates to the comparison of values in the objects. If a class does not override the equals method, then by default, it uses the equals(Object o) method of the closest parent class that has overridden this method.

Why is equals () false in Java?

The equals() method of Java Boolean class returns a Boolean value. It returns true if the argument is not null and is a Boolean object that represents the same Boolean value as this object, else it returns false.

Why does string defines equals () method instead of using ==?

That's all on the difference between the equals() method and == operator in Java. Both can compare objects for equality but equals() is used for logical and business logic comparison while == mostly for object reference comparison in Java.


1 Answers

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().

like image 92
Paul Boddington Avatar answered Sep 24 '22 15:09

Paul Boddington