13

Possible Duplicate:
How do I compare strings in Java?

I cant understand why the declared variables is not the same.

ex code:

 String firstPart = "F";
 String whole = "False";
 String connected = firstPart + "alse";
 System.out.println(connected == whole);

Now this produce a boolean and I thought it would be 'true' BUT it is not, It comes out as false and I don't understand why.

Can someone explain this?

Community
  • 1
  • 1
user1501127
  • 865
  • 1
  • 18
  • 32
  • 3
    +1. A fair enough question if you don't know about `equals()`, although using "False" as the example confuses the issue (people may be marking you down thinking you want to comparing strings with booleans) – SDC Jan 15 '13 at 13:40
  • 3
    It is a common mistake. But this question has probably been asked THOUSANDS of times before on SO. – Stephen C Jan 15 '13 at 13:41
  • Thanks for taking the time thou! It's greatly appreciated! – user1501127 Jan 15 '13 at 13:43
  • 2
    If you make `firstPart` final it will be the same object so `==` will be true. Otherwise only the *contents* will be the same so you have to use `equals` – Peter Lawrey Jan 15 '13 at 13:48
  • Strings in Java are immutable. So string a + string b will never == string c. Likely (string a + string b) will be allocated on the stack, while string c will be in the string pool. – Mikkel Løkke Jan 15 '13 at 13:58
  • The question is - if you're writing this kind of stuff in your code how will somebody else understand it? – Mukus Mar 06 '14 at 22:54
  • @TejaswiRana This is a homework question to illustrate how the compiler works rather then to point out the many other errors with the syntax ;) Since i learned alot it was not a bad question in it self. – user1501127 Mar 07 '14 at 10:57

8 Answers8

20

You are comparing references, not the values.

You'll need to use equals:

connected.equals(whole);
Femaref
  • 60,705
  • 7
  • 138
  • 176
17

This

String connected = firstPart + "alse";

creates a new String object, with a new underlying char array and a new reference.

Consequently when you compare references (using '=='') you won't get a match. If you compare the actual object contents using equals() then you'll get the result you want (since String.equals() compares the contents of the underlying char arrays)

Brian Agnew
  • 268,207
  • 37
  • 334
  • 440
  • Just because others will see this, due to String interning, if you make the String assignments final, then (connected == whole) is indeed true. – Quaternion Jan 24 '19 at 22:45
6

You should compare strings using equals(). Like so:

System.out.println(connected.equals(whole));

Ivaylo Strandjev
  • 69,226
  • 18
  • 123
  • 176
1

When you use "==" to compare the strings it means that you want to compare their reference values but not the values they have, which in this case will return false.

you need to use equals method which will compare strings based upon the values they are holding..

Pratik
  • 1,531
  • 3
  • 25
  • 57
1

You should use .equals() method to compare string...

Kanagaraj M
  • 956
  • 8
  • 18
1

Essentially, only PDTs can be checked with ==. These are byte, short, int, long, float, double, boolean, char and of course, references. As the two references you get are different objects, the result would be false.

For any ADT (or object), you have to make use of the equals() method. All Java objects have them and you have to implement it for your own objects as well. For the String object this has been done for you and the characters are compared for to test equality.

Jaco Van Niekerk
  • 4,180
  • 2
  • 21
  • 48
1

Java ain't JavaScript - the comparison operators are different:

In java == means "are the two objects the same instance?", which corresponds to JavaScript's === operator.

In java .equals() for String means "are the characters the same", which corresponds to JavaScript's == operator.

Bohemian
  • 412,405
  • 93
  • 575
  • 722
1

Rule of thumb: == compares object references, not object values. Overriding equals is the standard way to define equality amongst objects (String included since it is a class, not a primitive).

firstPart, whole and connected define a new String each, so their references are different even if connected and whole match the same String in the pool, because == will not contemplate value.

Fritz
  • 9,987
  • 4
  • 30
  • 49