1

I have a String response_content of length=1 in a java project and I know that it equals to 1 (ASCII 49). I know this, because the following lines (in Eclipse with android ADT+SDK)

Log.i("GET RESPONSE", response_content);
Log.i("GET RESPONSE", response_content.length());
Log.i("GET RESPONSE", response_content.codePointAt(0));

produce this output:

1
1
49

But why do these lines always return false?

if (response_content.equals(1)) {...}
if (response_content == "1") {...}

I know equals() is the adequate way, == is just for testing purposes. Is there another way of telling me, what the string really contains or is there a mistake I don't see?

Peder
  • 345
  • 1
  • 4
  • 14
  • 4
    == is not for testing purposes. its for testing references – Marco Acierno Oct 06 '14 at 17:59
  • 1
    == is used to compare instances (references), use equals()/equalsIgnoreCase() – Fevly Pallar Oct 06 '14 at 17:59
  • 1
    compare string as `response_content.equals("1");` – Rustam Oct 06 '14 at 18:00
  • You should make the first test as `response_content.equals("1")`. If not, what you do is to compare the String "1" with the boxed Integer value 1, which is false. In the second case, this is an example where you should not use == to test equality of Strings; always use `equals()` with another String. – Jean-Baptiste Yunès Oct 06 '14 at 18:00
  • `"1" == "1"` will always return false. – Cedric Reichenbach Oct 06 '14 at 18:01
  • 1
    No "1"=="1" will always return true! But if you test a reference to a String to another String literal or reference, then the test could be false (it depends on the way the Strings have been built). – Jean-Baptiste Yunès Oct 06 '14 at 18:04
  • 1
    @CedricReichenbach "1"=="1" will be true because Java does interning of string literals at compile time. Only strings generated at runtime will be new objects. – Teddy Oct 06 '14 at 18:11
  • Almost everyone suggested the right solution (I voted for the first to answer). I simply "forgot" the double quotes. That's been it. Thank you! – Peder Oct 06 '14 at 18:18
  • This is NOT a duplicate. A lot of people just don't read the question. I explicitly wrote "I know equals() is the adequate way". I don't know how this can be possibly misunderstood. – Peder Oct 06 '14 at 19:19
  • @Teddy Oh, my bad... This is somewhat messed up. Apart of performance benefits, does this make any sense? If you hardcode a string at two different places, I don'w see why you'd want it to be the same instance. – Cedric Reichenbach Oct 07 '14 at 08:07
  • Ya.. Its quite a source of confusion. But its necessary for efficient memory usage. Strings are immutable. So, it doesn't make any difference if java de-dupes the strings. Its useful to de-dupe strings to reduce memory usage. At compile time its easy to do that.. so Java does it. At runtime it will become a performance hit.. so Java doesnt do it. So, always use .equals for string comparison. It wont be a performance hit because .equals is smart enough to check for reference equality as the first step before starting to compare content. – Teddy Oct 07 '14 at 13:14

2 Answers2

4

The first one

if (response_content.equals(1)) {...}

is comparing a String object to an Integer object ... they don't match on object-type, so this evaluates false. The second one

if (response_content == "1") {...}

is comparing the reference, and not the data itself ... 'response_content' is a separate object from "1", so this evaluates false.

Try this instead:

if (response_content.equals("1")) {...}
mw_goodjava
  • 271
  • 1
  • 5
1

must be response_content.equals("1") otherwise a String object gets compared to an Integer object

response_content == "1" may return true but doesn't have to because those can be different "1" strings.

Alexander Kulyakhtin
  • 47,782
  • 38
  • 107
  • 158