Wednesday 15 May 2013

How to compare strings in java? -



How to compare strings in java? -

this question has reply here:

how compare strings in java? 23 answers

== tests reference equality.

.equals() tests value equality.

i saw post how compare strings in java? . , still don't why false when compare

// ... not same object new string("test") == "test" // --> false

or

string str1 = new string("java"); string str2 = new string("java"); system.out.println(str1==str2);

does happen because have different name or reason ?

that happens because == operator compares memory addresses, not contents.

note string object, not primitive, why people confused, can compare primitives == , fine, objects want compare contents.

java

No comments:

Post a Comment