4

Possible Duplicate:
Java string comparison?

I have encounter the following problem, I have an object called "lang", is a result from a method LanguageDetector.detect() which output a string.

lang = LanguageDetector.detect();

So I would want to check whether the language is english, so I am checking,

lang == "en"

The following screen is my debug screen, my lang is showing "en", however my lang == "en" is showing false and lang.toString() == "en" is false, does anyone encounter following problem before and have a possible solution?

Weird java problem

Community
  • 1
  • 1
drhanlau
  • 2,517
  • 2
  • 24
  • 42

7 Answers7

7

Use equals() method of String object instead of direct comparison.

String first = new String("Hello");
String second = new String("Hello");

first == second will return false.

first.equals(second) will return true.
Joel
  • 29,538
  • 35
  • 110
  • 138
  • 6
    Actually, in your example, (first == second) will return true also. That's because all literal strings are interned. For your example to work, you would need something like String second = new String("Hello"). – Nicolae Albu Jul 17 '11 at 10:45
  • I have edited the answer to reflect this. – Joel Jul 17 '11 at 13:30
5

In Java, == always does a reference comparison. You need a value comparison though (with the equals() method for instance).

Lucero
  • 59,176
  • 9
  • 122
  • 152
5

You're comparing the references to the Strings rather than the contents of the strings themselves. See here for more info.

Note that this issue doesn't apply just to Strings, but to all objects. As such, you may have to define appropriate equals() methods for any objects you create yourself.

Additionally String interning will confuse matters if you're not careful. See here for more details.

Brian Agnew
  • 268,207
  • 37
  • 334
  • 440
3

Use lang.equals("en") instead of lang == "en". The latter compares the two string references for equality, whereas the former compares the contents of the two strings.

See http://www.devdaily.com/java/edu/qanda/pjqa00001.shtml for an overview of different string comparison methods in Java.

NPE
  • 486,780
  • 108
  • 951
  • 1,012
3

By using == you are checking that both string references point to the same object.

For strings that are created on the fly, and not interned, this will equal false.

To compare the strings for equality, letter by letter, use string1.equals(string2) or even string1.equalsIgnoreCase(string2).

Joel
  • 29,538
  • 35
  • 110
  • 138
2

Use "en".equals(lang) instead of lang == "en"

Richard Kettelerij
  • 2,049
  • 14
  • 17
1

Its better to use the equals as said but if its necessary for performance reasons you can try the intern() function.

lang.intern() == "en"
Tom van der Woerdt
  • 29,532
  • 7
  • 72
  • 105
  • Interesting approach - but this is, unfortunately, not very idiomatic Java (i.e. - if you sent me this in a code review, I'd probably tell you to do it "right" instead). In this case, the range of possible values for lang is small - but if done indiscriminately could cause a blow-up of the intern'd string pool. – James Jul 18 '11 at 06:17