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