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

3

48

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

Meganmeganthropus answered 29/5, 2012 at 9:24 Comment(1)
It says == and 'eq' do the same thing, so they do the same thing.Broadleaved
S
48

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

Sohn answered 29/5, 2012 at 9:28 Comment(0)
S
29

According to documentation, it's the same thing

In addition to the . and [] operators discussed in Value and Method Expressions, the EL provides the following operators, which can be used in rvalue expressions only:
[...]
Relational: ==, eq, !=, ne, <, lt, >, gt, <=, ge, >=, le. Comparisons can be made against other values or against Boolean, string, integer, or floating-point literals.

One difference though : string operators like eq, ne, lt, gt, ge, le exist as they are XML safe, they would not need to be escaped like <= for instance.

It's explained here

A useful feature of the EL is the ability to perform comparisons, either between numbers or objects. This feature is used primarily for the values of custom tag attributes, but can equally be used to write out the result of a comparison (true or false) to the JSP page. The EL provides the following comparison operators:

• == or eq
• != or ne
• < or lt
• > or gt
• <= or le
• >= or ge

The second version of each operator exists to avoid having to use entity references in JSP XML syntax; however, the behavior of the operators is the same.

Spermine answered 29/12, 2014 at 11:49 Comment(0)
V
27

Both are same. Both == and eq will result in the following code:

jspContext.findAttribute("person.sokande_i").equals("endast_usa")

for EL

${person.sokande_i == 'endast_usa'}
Vladimir answered 29/5, 2012 at 9:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.