Groovy 關(guān)系運(yùn)算符

2018-12-29 14:00 更新

關(guān)系運(yùn)算符允許對(duì)象的比較。以下是Groovy中可用的關(guān)系運(yùn)算符 -

運(yùn)算符描述例子
==測(cè)試兩個(gè)對(duì)象之間的等同性2 == 2將得到true
!=測(cè)試兩個(gè)對(duì)象之間的差異3!= 2將得到true
<檢查左對(duì)象是否小于正確的操作數(shù)。2 <  3將得到true
<=檢查左對(duì)象是否小于或等于右操作數(shù)。2 <  3將得到true
>檢查左對(duì)象是否大于右操作數(shù)。3 > 2將得到true
>=檢查左對(duì)象是否大于或等于右操作數(shù)。3 = 2將得到true

以下代碼段顯示了如何使用各種運(yùn)算符。

class Example { 
   static void main(String[] args) { 
      def x = 5;
      def y = 10;
      def z = 8;
		
      if(x == y) { 
         println("x is equal to y"); 
      } else 
         println("x is not equal to y"); 
			
      if(z != y) { 
         println("z is not equal to y"); 
      } else 
         println("z is equal to y"); 
				
      if(z != y) { 
         println("z is not equal to y"); 
      } else 
         println("z is equal to y"); 
					
      if(z<y) { 
         println("z is less than y"); 
      } else 
         println("z is greater than y"); 
						
      if(x<=y) { 
         println("x is less than y"); 
      } else 
         println("x is greater than y"); 
			
      if(x>y) { 
         println("x is greater than y"); 
      } else 
         println("x is less than y"); 
			
      if(x>=y) { 
         println("x is greater or equal to y"); 
      } else 
         println("x is less than y"); 
   } 
} 

當(dāng)我們運(yùn)行上面的程序,我們將得到以下結(jié)果。可以看出,結(jié)果如從上面所示的操作符的描述所預(yù)期的。

x is not equal to y 
z is not equal to y 
z is not equal to y 
z is less than y
x is less than y 
x is less than y 
x is less than y 
以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)