==
and eq
give the same result using EL to do my string comparison tests:
<c:if test="${person.sokande_i == 'endast_usa'}">Endast USA</c:if> <c:if test="${person.sokande_i == 'alla'}">Alla länder</c:if> <c:if test="${person.sokande_i == 'alla_utom_usa'}">Alla utom USA</c:if>
Should I use eq
instead? Is ==
for integers only? But it works also for strings. AFAIK ==
test whether hashCodes are equal and eq
means "meaningfully different".
Another question says ==
and eq
do the same thing.
Is there no difference here? IS not the difference the one I'm stating: ==
looks at the hashCode and eq
looks at the implementation of equals(...)
.
You should not use == (equality operator) to compare these strings because they compare the reference of the string, i.e. whether they are the same object or not. On the other hand, equals() method compares whether the value of the strings is equal, and not the object itself.
The right way of comparing String in Java is to either use equals(), equalsIgnoreCase(), or compareTo() method. You should use equals() method to check if two String contains exactly same characters in same order. It returns true if two String are equal or false if unequal.
Using the “==” operator for comparing text values is one of the most common mistakes Java beginners make. This is incorrect because “==” only checks the referential equality of two Strings, meaning if they reference the same object or not.
The 2 main differences are that: equals will take any Object as a parameter, but compareTo will only take Strings. equals only tells you whether they're equal or not, but compareTo gives information on how the Strings compare lexicographically.
They're both the same. I use eq
in EL as it is more readable like a sentence.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With