Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I choose == or eq for comparing string in EL?

== 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(...).

like image 216
Niklas Rosencrantz Avatar asked May 29 '12 09:05

Niklas Rosencrantz


People also ask

Can you use == when comparing strings?

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.

What is the best way to compare strings?

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.

When comparing strings Why is == not a good idea?

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.

What is difference between == equals () and compareTo () method?

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.


1 Answers

They're both the same. I use eq in EL as it is more readable like a sentence.

like image 92
adarshr Avatar answered Sep 23 '22 08:09

adarshr