Operators in SpEL by R4R Team

Operators in SpEL:-
We can use many operators in SpEL such as arithmetic, relational, logical etc. There are given a lot of examples of using different operators in SpEL.

Relational operators

The relational operators; equal, not equal, less than, less than or equal, greater than, and greater than or equal are supported using standard operator notation.

//evaluates to true

boolean trueValue = parser.parseExpression("3 == 3").getValue(Boolean.class);

//evaluates to false

boolean falseValue = parser.parseExpression("2 < -1.0").getValue(Boolean.class);

//evaluates to true

boolean trueValue = parser.parseExpression("'bay' < 'boy'").getValue(Boolean.class);

Logical operators

The logical operators that are supported are and, or, and not. Their use is below.

//-- AND --

//evaluates to false

boolean falseValue = parser.parseExpression("true and false").getValue(Boolean.class);

//evaluates to true

String expression = "isMember('Vipul Sharma') and isMember('Employee')";

boolean trueValue = parser.parseExpression(expression).getValue(societyContext, Boolean.class);

//-- OR --

//evaluates to true

boolean trueValue = parser.parseExpression("true or false").getValue(Boolean.class);

//evaluates to true

String expression = "isMember('Vipul Sharma') or isMember('Social Service')";

boolean trueValue = parser.parseExpression(expression).getValue(societyContext, Boolean.class);

//-- NOT --

//evaluates to false

boolean falseValue = parser.parseExpression("!true").getValue(Boolean.class);

//-- AND and NOT --

String expression = "isMember('Vipul Sharma') and !isMember('Employee')";

boolean falseValue = parser.parseExpression(expression).getValue(societyContext, Boolean.class);

Mathematical operators

The addition operator can be used on both numbers and strings. Subtraction, multiplication and division can be used only on numbers. Other mathematical operators supported are modulus (%) and exponential power (^).
 

//Addition

int num = parser.parseExpression("5 + 1").getValue(Integer.class); // 6

String testString = parser.parseExpression(

"'hello' + ' ' + 'spring'").getValue(String.class); // hello spring

//Subtraction

int num = parser.parseExpression("2 - -3").getValue(Integer.class); // 5

double d = parser.parseExpression("1000.00 - 1e4").getValue(Double.class); // -9000

//Multiplication

int num = parser.parseExpression("-3 * -3").getValue(Integer.class); // 9

double num = parser.parseExpression("2.0 * 3e0 * 4").getValue(Double.class); // 24.0

//Division

int num = parser.parseExpression("6 /2").getValue(Integer.class); // 3

double num = parser.parseExpression("8.0 / 4e0 / 2").getValue(Double.class); // 1.0

//Modulus

int num = parser.parseExpression("7 % 2").getValue(Integer.class); // 1

int num = parser.parseExpression("8 / 5 % 2").getValue(Integer.class); // 1

//Operator precedence

int num = parser.parseExpression("1+2-3*4").getValue(Integer.class); // -9

Leave a Comment:
Search
Categories
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!