EL OperatorsS2C Home « EL Operators
Symbols used for mathematical and logical manipulation that are recognized by the compiler are commonly known as operators in Java and with EL we also have a comprehensive, although reduced, list of operators. In
this lesson we look at the arithmetic, relational, logical, conditional and empty
EL operators we can use within our JSP pages.
Arithmetic OperatorsTop
We are familiar with most of the arithmetic operators in the table below from everyday life and they generally work in the same way in EL.
Operator | Meaning | Example | Notes |
---|---|---|---|
+ | Addition | ${a + b} | |
- | Subtraction | ${a - b} | |
/ div | Division | ${a / b} | Any remainder will be truncated. |
* | Multiplication | ${a * b} | |
% mod | Modulus | ${a % b} | Holds the remainder value of a division. |
Relational OperatorsTop
Relational Operators refer to the relationships that values can have to each other. Relational Operators produce a true
or false
result. Any type can be compared for equality or inequality
but only types that support an ordering relationship can be applied for comparison. The table below clarifies this where a = 5
and b = 5
Operator | Meaning | Example | Result | Notes |
---|---|---|---|---|
== eq | Equal to | ${a == b} | true | All types can be compared for equality |
!= ne | Not Equal to | ${a != b} | false | All types can be compared for inequality |
< lt | Less than | ${a < b} | false | Can be used with all numeric types and the char type. |
<= le | Less than or equal to | ${a <= b} | true | Can be used with all numeric types and the char type. |
> gt | Greater than | ${a > b} | false | Can be used with all numeric types and the char type. |
>= ge | Greater than or equal to | ${a >= b} | true | Can be used with all numeric types and the char type. |
Logical OperatorsTop
Logical Operands must be the Boolean
type and the result of a logical operation is the Boolean
type and are used with control statements. The following table shows all possible combinations and their result.
Operator | Meaning | Example | Result | Notes |
---|---|---|---|---|
&& | Short-circuit AND | Boolean a = false; Boolean b = false;${a && b} Boolean a = false; Boolean b = true; ${a && b} Boolean a = true; Boolean b = false; ${a && b} Boolean a = true; Boolean b = true; ${a && b} |
false false false true | If the first operand returns false , the second operand will not be checked (short-circuited) and false is returned. |
|| | Short-circuit OR | Boolean a = false; Boolean b = false;${a || b} Boolean a = false; Boolean b = true; ${a || b} Boolean a = true; Boolean b = false; ${a || b} Boolean a = true; Boolean b = true; ${a || b} |
false true true true | If the first operand returns true , the second operand will not be checked (short-circuited) and true is returned. |
! | NOT | Boolean a = false;${!a} Boolean a = true; ${!a} |
true false | Will check if operand is not true . |
The Conditional OperatorTop
The Conditional operatotor is tenary (takes three operands) and can be used to replace an if....else
construct of the following form:
Construct | Description |
---|---|
if....else | |
if (condition) { | Assign result of expression1 to
var if condition evaluates to true ,otherwise assign result of expression2 to var. |
The Conditional Operator | |
${statement? expression1 : expression2 |
If statement evaluates to true evaluate expression1 ,
otherwise evaluate expression2 , |
The following code snippet shows how to use the Conditional
operator which for this code will result in higher number
being output:
<%
request.setAttribute("a", 15);
request.setAttribute("b", 6);
%>
${(requestScope.a lt requestScope.b)? "lower number" : "higher number"}
The empty
OperatorTop
The empty
operator can be used to test the emptiness of an object and has the following syntax:
${empty obj}
The empty
operator will return true
when any of the following criteria is met, otherwise it will return false
:
- If the object being tested is
null
. - If the object being tested is an empty string (
""
). - If the object being tested is an empty array.
- If the object being tested is an empty Map or Collection.
Operator PrecedenceTop
The table below shows the order of precedence that is invoked when the container interprets EL operator symbols, including the reserved word operators. When using multiple operators in an expression it is always best practice to use parentheses to explicitly state the order of precedence for clarity and readability. The table lists order of precedence from highest to lowest.
Precedence | Operators | Operation | Associativity |
---|---|---|---|
1 | ! | boolean logical NOT | right |
not | |||
2 | empty | left | |
3 | / | division | left |
div | |||
* | multiplication | ||
% | modulus | ||
mod | |||
4 | + | addition or string concatenation | left |
- | subtraction | ||
5 | < | less than | left |
lt | |||
<= | less than or equal to | ||
le | |||
> | greater than | ||
gt | |||
>= | greater than or equal to | ||
ge | |||
instanceof | reference test | ||
6 | == | equal to | left |
eq | |||
!- | not equal to | ||
ne | |||
7 | && | short-cricuit boolean logical AND | left |
and | |||
8 | || | short-cricuit boolean logical OR | left |
or |
Lesson 2 Complete
In this lesson we learnt about the symbols used in EL for mathematical and logical manipulation which are known as EL operators.
What's Next?
In the next lesson we learn how to use EL to access object properties and attribute values from any scope.