Nearby lessons
14 of 30JSP - EL Operators
📌 What You Will Learn
- Use property access (.) and collection access ([]) operators
- Master arithmetic, relational, and logical operators
- Understand the empty operator and EL null handling
EL Operators provide arithmetic, relational, logical, and conditional operations directly in EL expressions. This lesson covers all EL operators, the empty operator, and operator precedence.
1) Property Access Operator (.)
Syntax: ${left.right}
- Left side: a Map or a Bean
- Right side: a Map key or Bean property (must be a valid Java identifier)
Example01
2) Collection Access Operator ([])
Syntax: ${left[right]}
Works with Maps, Beans, Arrays, and Lists.
| Left Type | Right Type | Quotes Required? |
|---|---|---|
| Map | Key | Yes — must use single or double quotes |
| Bean | Property | Yes |
| Array | Index | No — quotes optional |
| List | Index | No — quotes optional |
Example02
3) Arithmetic Operators
| Operator | Symbol | Keyword | Example | Result |
|---|---|---|---|---|
| Addition | + | — | ${2+3} | 5 |
| Subtraction | - | — | ${10-3} | 7 |
| Multiplication | * | — | ${10*3} | 30 |
| Division | / | div | ${10/2} | 5.0 (always floating-point) |
| Modulus | % | mod | ${10%3} | 1 |
Arithmetic Rules
- No string concatenation with
+— it always means addition. EL does not support operator overloading. - Null is treated as 0 in arithmetic:
${10+null}= 10 - Empty string is treated as 0:
${""+3}= 3 - Non-numeric strings cause NumberFormatException:
${"abc"+3}= RE - Undefined attributes are treated as 0:
${abc+3}= 3 - Division always follows floating-point arithmetic:
${10/2}= 5.0,${10/0}= Infinity - Modulus follows integral arithmetic:
${10%0}= ArithmeticException
4) Relational Operators
| Operator | Symbol | Keyword | Example | Result |
|---|---|---|---|---|
| Greater than | > | gt | ${10>3} | true |
| Less than | < | lt | ${10<3} | false |
| Equal | == | eq | ${abcd==abc} | true |
| Greater or equal | >= | ge | ${10>=10} | true |
| Less or equal | <= | le | ${5<=3} | false |
| Not equal | != | ne | ${10!=3} | true |
5) Logical Operators
| Operator | Symbols | Keyword | Example |
|---|---|---|---|
| AND | &, && | and | ${true && false} → false |
| OR | |, || | or | ${true || false} → true |
| NOT | ! | not | ${!true} → false |
6) Conditional Operator
Syntax: ${condition ? trueValue : falseValue}
Example07
7) Empty Operator
Syntax: ${empty object}
Returns true if the object:
- Does not exist (is null)
- Is an empty string
- Is an empty array or collection
Returns false otherwise.
Example08
EL Operator Precedence
From highest to lowest:
- Unary:
!,empty - Multiplicative:
*,/,% - Additive:
+,- - Relational:
>,<,>=,<= - Equality:
==,!= - Logical AND:
&&,and - Logical OR:
||,or - Conditional:
? :
EL Reserved Words
These words cannot be used as identifiers in EL:
true · false · null · empty · instanceof · gt · lt · ge · le · ne · eq · and · or · mod · div · not
EL vs Null
EL handles null very gracefully:
- Arithmetic: null is treated as zero
- Strings: null is treated as empty string
- Logical: null is treated as false
Example11
📝 Key Takeaways
- . accesses map keys or bean properties; [] accesses map keys, bean properties, or array/list indices
- + always means addition — no string concatenation in EL
- empty returns true for null, empty strings, empty arrays, and empty collections
🧠 Test Your Knowledge
3 QuestionsProgress: 0 / 3