Arithmetic Operators
The EL supports the following arithmetic operators.
1. Addition +
2. Subtraction –
3. Multiplication *
4. Division / and div
5. Modulo division % and mod
Addition + : ExpressionLanguage doesn’t support operator overloading hence “+” operator always meant for arithmetic addition operator.
Ex:
1. ${2+3} Output: 5
2. ${“2”+ ‘3’} Output: 5
3. ${“2” + ‘3’} Output: 5
4. ${abc+ ‘3’} Output: 3
5. ${null+ “3”} Output: 3
6. ${“abc” + “3”} Output: NumberFormatException
7. ${“abc” + “3”} Output: NumberFormatException
Subtraction - :
All rules are exactly same as ‘+’ operator.
Ex:
1. ${10-3} Output : 7
2. ${“abc”-3} Output : NumberFormatException.
3. ${abc-3} Output 3
Multiplication * :
All rules are exactly similar to “+” operator.
Ex:
1. $ {10*3} Output: 30
2. ${“abc”*3} Output: NumberFormatException.
Division / and div :
All the rules are exactly same as ‘+’ operator.
Syntax: ${a/b} (or) ${a div b}
Ex:
1. ${10/2} Output : 5.0
2. ${10/0} Output : Infinity
Note : Division operator always follows floating point arithmetic but not integral arithmetic.
System.out.println (10/0) Output: ArithmeticException / Zero
System.out.println (1.0/0) Output : Infinity
System.out.println (0/0) Output :ArithmeticException
System.out.println (0/0.0) Output :NaN
Modulo division % and mod :
All the rules are exactly same as ‘+’ operator.
And this operator can provide can support for both integral and floating point arithmetic.
Ex:
1. $ {10%3} Output : 1
2. $ {10%0} Output : ArithmeticException
3. $ {10.0%0} Output : NaN
Note: / à Floating point Arithmetic.
% à Integral & Floating point arithmetic.
No comments:
Post a Comment