Advertisement



< Prev
Next >



SpEL Operators



In this tutorial, we will explain how Spring Expression Language(SpEL) allows us to use different kinds of operators to form its expressions. These operators are - Let's see how each of these operators are used to form expressions in Spring Expression Language(SpEL) by coding examples.


Adding JARs










Example
package decodejava;

import org.springframework.expression.spel.standard.SpelExpressionParser;

public class Utility {

	public static void main(String[] args) 
	{
		SpelExpressionParser parser = new SpelExpressionParser();
		
		//Ternary operator ?:  with SpEL
		System.out.println("100 > 50 : " + parser.parseRaw("100 > 50 ? true:false").getValue());
		System.out.println("200 < 100 : " + parser.parseRaw("200 < 100 ? true:false").getValue());
		System.out.println("100 == 99.9 : " + parser.parseRaw("100 == 100 ? true:false").getValue());
		System.out.println("Adam == Adam : " + parser.parseRaw("'Adam' == 'Adam' ? true:false").getValue());
		System.out.println("Moon == Moons : " + parser.parseRaw("'Moon' == 'Moons' ? true:false").getValue());
		
		
		//Type operator used to invoke static methods of references type String and Wrapper Classes
		System.out.println(parser.parseRaw("T(String).valueOf(10)").getValue());
		System.out.println(parser.parseRaw("T(Integer).parseInt(1001,2)").getValue());
		System.out.println(parser.parseRaw("T(Double).parseDouble('99.9')").getValue());
		System.out.println("Minimum value between 99.2 and 99.9 : " + parser.parseRaw("T(Math).min(99.2, 99.9)").getValue());
		System.out.println("Maximum value between 99.2 and 99.9 : " + parser.parseRaw("T(Math).max(99.2, 99.9)").getValue());

		
		//instanceOf operator
		System.out.println("Is Hello an instance of String : " + parser.parseRaw("'Hello' instanceOf T(String)").getValue());
		System.out.println("Does new String() creates an Instance of String : " + parser.parseRaw("new String() instanceOf T(String)").getValue());
		System.out.println("Is 10 an object of Integer : " + parser.parseRaw("10 instanceOf T(Integer)").getValue());
		System.out.println("Is 6.5 an object of Double : " + parser.parseRaw("6.5 instanceOf T(Float)").getValue());
		System.out.println("Is 9.9 an object of Double : " + parser.parseRaw("9.9 instanceOf T(Double)").getValue());
		
		
		
	}
}


Output
100 > 50 : true
200 < 100 : false
100 == 99.9 : true
Adam == Adam : true
Moon == Moons : false
10
9
99.9
Minimum value between 99.2 and 99.9 : 99.2
Maximum value between 99.2 and 99.9 : 99.9
Is Hello an instance of String : true
Does new String() creates an Instance of String : true
Is 10 an object of Integer : true
Is 6.5 an object of Double : false
Is 9.9 an object of Double : true


This concludes our tutorial on how Spring Expression Language(SpEL) uses different kinds of operators to form its expressions. In our next tutorial, we will explain how to access methods(static and non-static) of a class using the Spring Expression Language(SpEL).




Please share this article -




< Prev
Next >
< SpEL variables
SpEL calling methods >



Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement



Notifications



Please check our latest addition

C#, PYTHON and DJANGO


Advertisement