0

Possible Duplicate:
How do I compare strings in Java?
Strings in Java : equals vs ==

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?

Community
  • 1
  • 1
CodyBugstein
  • 21,984
  • 61
  • 207
  • 363
  • what if it wasn't a problem with uppercase? – bluesman Sep 20 '12 at 18:58
  • 1
    You need to learn the difference between `==` and `String.equals()`. – jahroy Sep 20 '12 at 18:59
  • Your mistaken - the == usually does work as expected for identical Strings – CodyBugstein Sep 20 '12 at 19:43
  • @Imray `==` works "as expected" only if you pass the same string objects around or work with interned strings. You can convince yourself that `new String("hi") == new String("hi")` is false by running a simple test. – trutheality Sep 20 '12 at 20:02
  • @Imray BTW, `"hi" == "hi"` is true because the compiler is clever enough to save space and actually use the same object for both `"hi"`s – trutheality Sep 20 '12 at 20:05

5 Answers5

8

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

boolean c = a.equals(b);
Alex
  • 25,147
  • 6
  • 59
  • 55
4

String.toUpperCase() creates a new object so

boolean c = (a==b);

will be false as you're comparing 2 different String references here.

You can use

boolean c = a.equals(b);

to check String content.

Reimeus
  • 158,255
  • 15
  • 216
  • 276
  • But if you create two String objects with exactly the same content, the == does work properly on them. Therefore, I don't believe the == is comparing references. – CodyBugstein Sep 20 '12 at 19:41
  • It will _only_ work (return true) if there is no conversion is done in so the same object is returned and the other object reference is the same. Here a new object is returned from `a.toUpperCase()` but not from `b.toUpperCase()`. The `==` operator always compares references. – Reimeus Sep 20 '12 at 19:53
1

Here '=' checks the reference and .equals compare the actual content

public static void main(String[] args) {

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

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

    boolean c = (a.equals(b));

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

If you want to use '=' then try this:

public static void main(String[] args) {

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

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

    boolean c = (a==b);

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

Thanks

Shreyos Adikari
  • 12,348
  • 19
  • 73
  • 82
0

It's the string compare that's not right, not the toUpperCase()

Do this instead:

boolean c = a.equals(b);

This is because the objects are equal by their values, but you still have two different object instances.

mprivat
  • 21,582
  • 4
  • 54
  • 64
0

You're comparing two string object references(which obviously aren't the same). Instead you could just use a.equal(b)

So your edit should be :

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

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

boolean c = (a.equals(b));

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

}

Aniket Inge
  • 25,375
  • 5
  • 50
  • 78