Search code examples
androidnanohttpd

The string contain "/" can't be compared in android?


I think Method A will displays "Ok", but in fact it displays "Fails". The Method B can get the correct result "OK".

I'm sure that the function fi.iki.elonen.NanoHTTPD.getMimeTypeForFile("my.css") will return the result "text/css".

I don't understand why the Method A can't get correct result. Is there some bugs with the function fi.iki.elonen.NanoHTTPD.getMimeTypeForFile ?

BTW, Method C can get the correct result "OK".

Method A

String a="text/css";
String b= fi.iki.elonen.NanoHTTPD.getMimeTypeForFile("my.css");

Utility.LogError("B: "+b);

if (a==b){
    Utility.LogError("Ok");
}else{
    Utility.LogError("Fails");
}

Method B

   String a="text/css";
    String b= fi.iki.elonen.NanoHTTPD.getMimeTypeForFile("my.css");

    Utility.LogError("B: "+b);

    if (a.compareTo(b)==0){
        Utility.LogError("Ok");
    }else{
        Utility.LogError("Fails");
    }

Method C

   String a="text/css";
   String b= "text/css";

    Utility.LogError("B: "+b);

    if (a==b){
        Utility.LogError("Ok");
    }else{
        Utility.LogError("Fails");
    }

Solution

  • Method 1

    It results to "Fails" It because the actual objects on heap are getting compared when you use == reference : Detailed explanation

    Method 2

    It results in Ok as a and b contain same text(mime type) in them (using compare to)

    Method 3

    It results in Ok, as expected.