표준 연산자 표기법을 사용하여 관계 연산자 (같음, 같지 않음, 같지 않음, 작음 이하, 더 크고 같음)가 지원됩니다. 다음 목록은 연산자의 몇 가지 예를 보여줍니다.
// evaluates to trueboolean trueValue =parser.parseExpression("2 == 2").getValue(Boolean.class);// evaluates to falseboolean falseValue =parser.parseExpression("2 < -5.0").getValue(Boolean.class);// evaluates to trueboolean trueValue =parser.parseExpression("'black' < 'block'").getValue(Boolean.class);
보다 크거나 작은 null에 대한 비교는 단순한 규칙 을 따른다 : null아무것도 아닌 것으로 취급된다 (그것은 0이 아니다). 결과적으로 다른 값은 항상 null 보다 크고 ( X > null항상 true) 아무것도없는 것보다 적은 값은 없습니다. ( X < null항상 false).
대신 숫자 비교를 선호하는 경우 0 에 대한 비교를 위해 숫자 기반 null비교를 사용하지 마십시오. (예 : X > 0또는 X < 0).
표준 관계 연산자 외에도 SpEL은 instanceof와 정규 표현식 기반 matches연산자를 지원합니다 . 다음 목록은 두 가지 예를 보여줍니다.
Java에서 super타입에 대한 연산을 위해 instanceof를 표현식으로 사용할 수 있다.
matches를 이용한 정규표현식을 지원한다.
// evaluates to falseboolean falseValue =parser.parseExpression("'xyz' instanceof T(Integer)").getValue(Boolean.class);// evaluates to trueboolean trueValue =parser.parseExpression("'5.00' matches '^-?\\d+(\\.\\d{2})?$'").getValue(Boolean.class);//evaluates to falseboolean falseValue =parser.parseExpression("'5.0067' matches '^-?\\d+(\\.\\d{2})?$'").getValue(Boolean.class);
프리미티브 유형은 래퍼 유형에 따라 즉시 박스 처리되므로 1 instanceof T (Integer)가 예상대로 true로 평가되는 동안 1 instanceof T (int)는 false로 평가됩니다.
Wrapper 클래스
자바의 데이터 타입은 다음의 표와 같이 프리미트브 타입과 레퍼런스 타입으로 나뉜다.
프리미티브 타입은 수치, 문자등 간단한 데이터를 표현할 수 있는 타입이고 그밖의 제이터 타입은 모두 레퍼런스 타입으로 분류된다.
경우에 다라서는 프리미티브 타입의 데이터를 객체로 만들어야할 필요가 있는데 이러한경우 사용하는게 Wrapper클래스이다.
reason - 위의 예제에서는 double 타입의 프리미티브값을 넘겨 Double 타입 파라미터를 받는 메소드를 호출하였습니다. 이러한 경우 double타입의 프리미트브 값을 갖는 Double 객체가 자동으로 생성되어 메소드에 넘겨집니다
각 기호 연산자는 순전히 알파벳순으로 지정할 수도 있습니다. 이렇게하면식이 포함 된 문서 형식 (예 : XML 문서)에 사용 된 기호가 특별한 의미를 갖는 문제를 피할 수 있습니다. 텍스트와 동등한 내용은 다음과 같습니다.
lt( <)
gt( >)
le( <=)
ge( >=)
eq( ==)
ne( !=)
div( /)
mod( %)
not( !).
모든 텍스트 연산자는 대소 문자를 구분하지 않습니다.
논리 연산자
SpEL은 다음과 같은 논리 연산자를 지원합니다 :
and
or
not
다음 예에서는 논리 연산자를 사용하는 방법을 보여줍니다.
// -- AND --// evaluates to falseboolean falseValue =parser.parseExpression("true and false").getValue(Boolean.class);// evaluates to trueString expression ="isMember('Nikola Tesla') and isMember('Mihajlo Pupin')";boolean trueValue =parser.parseExpression(expression).getValue(societyContext,Boolean.class);// -- OR --// evaluates to trueboolean trueValue =parser.parseExpression("true or false").getValue(Boolean.class);// evaluates to trueString expression ="isMember('Nikola Tesla') or isMember('Albert Einstein')";boolean trueValue =parser.parseExpression(expression).getValue(societyContext,Boolean.class);// -- NOT --// evaluates to falseboolean falseValue =parser.parseExpression("!true").getValue(Boolean.class);// -- AND and NOT --String expression ="isMember('Nikola Tesla') and !isMember('Mihajlo Pupin')";boolean falseValue =parser.parseExpression(expression).getValue(societyContext,Boolean.class);
수학 연산자
더하기 (+) 연산자를 숫자, 날짜, 문자열에 사용할 수 있다.
빼기(-) 연산자를 숫자, 날짜에 사용할 수 있다.
곱하기(*), 나누기(/), 나머지(%)에 대한 연산은 숫자에만 사용 할 수 있다.
연산자 우선 순위 법칙이 적용된다.
숫자와 문자열 모두에서 더하기 연산자를 사용할 수 있습니다. 빼기, 곱하기 및 나누기 연산자는 숫자에만 사용할 수 있습니다. 모듈러스 (%) 및 지수 출력 (^) 연산자를 사용할 수도 있습니다. 표준 운영자 우선 순위가 적용됩니다. 다음 예제는 사용중인 수학 연산자를 보여줍니다.