Wednesday 23 January 2019

java - toUpperCase not functioning as I'd expect











I have two Strings; one is "hello" in lower case and one is "HELLO" in upper.




When I apply toUpperCase to the variables and then use a boolean to compare them, they are coming out as not equal and I can't figure out why.



public static void main(String[] args) {

String a = "hello";
String b = "HELLO";

a = a.toUpperCase();
b = b.toUpperCase();


boolean c = (a==b);

System.out.println(b + " " + a + " " + c);
}


The output is HELLO HELLO false but it should be HELLO HELLO true. Shouldn't it?
What am I missing?


Answer




toUpperCase is working correctly. You have to use equals to check for equality of both Strings.



boolean c = a.equals(b);

No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...