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}
${a div b}
Any remainder will be truncated.
*Multiplication${a * b}
%
mod
Modulus${a % b}
${a mod 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}
${a eq b}
trueAll types can be compared for equality
!=
ne
Not Equal to${a != b}
${a ne b}
falseAll types can be compared for inequality
<
lt
Less than${a < b}
${a lt b}
falseCan be used with all numeric types and the char type.
<=
le
Less than or equal to${a <= b}
${a le b}
trueCan be used with all numeric types and the char type.
>
gt
Greater than${a > b}
${a gt b}
falseCan be used with all numeric types and the char type.
>=
ge
Greater than or equal to${a >= b}
${a ge b}
trueCan 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
&&
and
Short-circuit ANDBoolean 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.
||
or
Short-circuit ORBoolean 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
NOTBoolean 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) {
    var = expression1;
} else {
    var = expression2;
}

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 NOTright
not
2emptyleft
3/divisionleft
div
*multiplication
%modulus
mod
4+addition or string concatenationleft
-subtraction
5<less thanleft
lt
<=less than or equal to
le
>greater than
gt
>=greater than or equal to
ge
instanceofreference test
6==equal toleft
eq
!-not equal to
ne
7&&short-cricuit boolean logical ANDleft
and
8||short-cricuit boolean logical ORleft
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.

go to home page Homepage go to top of page Top