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?
This
creates a new
Stringobject, 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 (sinceString.equals()compares the contents of the underlying char arrays)