Wednesday, January 23, 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

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...