In EL (${ ... }), you can use many types of operators — very similar to Java, but slightly simplified.
Here’s the full organized list:
🔵 1. Arithmetic Operators
| Operator | Description | Example |
|---|---|---|
+ | Addition | ${2 + 3} → 5 |
- | Subtraction | ${5 - 2} → 3 |
* | Multiplication | ${4 * 2} → 8 |
/ or div | Division | ${10 / 2} or ${10 div 2} → 5 |
% or mod | Modulus (remainder) | ${10 % 3} or ${10 mod 3} → 1 |
✅ div and mod are alternative keywords for / and % (useful when writing XML-compliant EL inside XHTML).
🔵 2. Relational (Comparison) Operators
| Operator | Description | Example |
|---|---|---|
== or eq | Equal to | ${5 == 5} or ${5 eq 5} → true |
!= or ne | Not equal to | ${5 != 3} or ${5 ne 3} → true |
< or lt | Less than | ${3 < 5} or ${3 lt 5} → true |
> or gt | Greater than | ${5 > 3} or ${5 gt 3} → true |
<= or le | Less than or equal to | ${5 <= 5} or ${5 le 5} → true |
>= or ge | Greater than or equal to | ${5 >= 3} or ${5 ge 3} → true |
✅ Again, the text forms like eq, lt, gt are XML-friendly.
🔵 3. Logical Operators
| Operator | Description | Example |
|---|---|---|
&& or and | Logical AND | ${(5 > 2) && (3 < 4)} or ${(5 gt 2) and (3 lt 4)} → true |
| ` | oror` | |
! or not | Logical NOT | ${!(5 > 6)} or ${not (5 gt 6)} → true |
🔵 4. Empty Operator
| Operator | Description | Example |
|---|---|---|
empty | Checks if a value is null, empty string, empty collection, or empty array | ${empty userList} |
✅ If userList is null or empty, empty userList returns true.
Very useful for checking nulls safely!
🔵 5. Conditional Operator (Ternary)
Just like Java’s ? : operator.
| Syntax | Example |
|---|---|
condition ? valueIfTrue : valueIfFalse | ${(age > 18) ? 'Adult' : 'Minor'} |
✅ Simple way to make decisions inside EL.
🎯 Final Full List:
| Operator Type | Examples |
|---|---|
| Arithmetic | +, -, *, / (div), % (mod) |
| Relational (Comparison) | ==, !=, <, >, <=, >= (eq, ne, lt, gt, le, ge) |
| Logical | &&, ` |
| Empty Test | empty |
| Conditional (Ternary) | condition ? value1 : value2 |
📢 Important Notes:
- EL is null-safe. If you reference something that doesn’t exist, it won’t throw an exception — it simply treats it as empty or null.
- Word operators (
eq,lt,and, etc.) are mainly there to be XML-compliant (some HTML parsers break on<or>symbols).
🚀 Super Quick Example Mixing Them:
<c:if test="${not empty user and user.age gt 18}">
<p>Welcome, ${user.name}! You are an adult.</p>
</c:if>
Checks if user exists and is older than 18.
Greets the user if conditions are met.