📜  Groovy-操作员

📅  最后修改于: 2020-11-04 06:36:12             🧑  作者: Mango


运算符是一个符号,告诉编译器执行特定的数学或逻辑操作。

Groovy具有以下类型的运算符-

  • 算术运算运算符
  • 关系运算符
  • 逻辑运算符
  • 按位运算符
  • 赋值运算符

算术运算符

Groovy语言支持普通的算术运算运算符,就像任何一种语言一样。以下是Groovy中可用的算术运算运算符-

显示范例

Operator Description Example
+ Addition of two operands 1 + 2 will give 3
Subtracts second operand from the first 2 − 1 will give 1
* Multiplication of both operands 2 * 2 will give 4
/ Division of numerator by denominator 3 / 2 will give 1.5
% Modulus Operator and remainder of after an integer/float division 3 % 2 will give 1
++ Incremental operators used to increment the value of an operand by 1

int x = 5;

x++;

x will give 6

Incremental operators used to decrement the value of an operand by 1

int x = 5;

x–;

x will give 4

关系运算符

关系运算符允许对象的比较。以下是Groovy中可用的关系运算符-

显示范例

Operator Description Example
== Tests the equality between two objects 2 == 2 will give true
!= Tests the difference between two objects 3 != 2 will give true
< Checks to see if the left objects is less than the right operand. 2 < 3 will give true
<= Checks to see if the left objects is less than or equal to the right operand. 2 <= 3 will give true
> Checks to see if the left objects is greater than the right operand. 3 > 2 will give true
>= Checks to see if the left objects is greater than or equal to the right operand. 3 >= 2 will give true

逻辑运算符

逻辑运算符用于评估布尔表达式。以下是Groovy中可用的逻辑运算符-

显示范例

Operator Description Example
&& This is the logical “and” operator true && true will give true
|| This is the logical “or” operator true || true will give true
! This is the logical “not” operator !false will give true

按位运算符

Groovy提供了四个按位运算运算符。以下是Groovy中可用的按位运算运算符-

显示范例

Sr.No Operator & Description
1

&

This is the bitwise “and” operator

2

|

This is the bitwise “or” operator

3

^

This is the bitwise “xor” or Exclusive or operator

4

~

This is the bitwise negation operator

这是展示这些运算符的真值表。

p q p & q p | q p ^ q
0 0 0 0 0
0 1 0 1 1
1 1 1 1 0
1 0 0 1 1

赋值运算符

Groovy语言还提供了赋值运算符。以下是Groovy中可用的赋值运算符-

显示范例

Operator Description Example
+= This adds right operand to the left operand and assigns the result to left operand.

def A = 5

A+=3

Output will be 8

-= This subtracts right operand from the left operand and assigns the result to left operand

def A = 5

A-=3

Output will be 2

*= This multiplies right operand with the left operand and assigns the result to left operand

def A = 5

A*=3

Output will be 15

/= This divides left operand with the right operand and assigns the result to left operand

def A = 6

A/=3

Output will be 2

%= This takes modulus using two operands and assigns the result to left operand

def A = 5

A%=3

Output will be 2

范围运算符

Groovy支持范围的概念,并在..表示法的帮助下提供范围运算符的表示法。下面给出了范围运算符的一个简单示例。

def range = 0..5 

这只是定义了一个简单的整数范围,存储在一个名为range的局部变量中,其下限为0,上限为5。

以下代码段显示了如何使用各种运算符。

class Example { 
   static void main(String[] args) { 
      def range = 5..10; 
      println(range); 
      println(range.get(2)); 
   } 
}

当我们运行上面的程序时,我们将得到以下结果-

println语句中,您可以看到显示了range语句中定义的整个数字范围。

get语句用于从定义的范围中获取一个对象,该对象以索引值作为参数。

[5, 6, 7, 8, 9, 10] 
7

运算符优先级

下表按优先顺序列出了所有常规运算符。

Sr.No Operators & Names
1

++ — + –

pre increment/decrement, unary plus, unary minus

2

* / %

multiply, div, modulo

3

+ –

addition, subtraction

4

== != <=>

equals, not equals, compare to

5

&

binary/bitwise and

6

^

binary/bitwise xor

7

|

binary/bitwise or

8

&&

logical and

9

||

logical or

10

= **= *= /= %= += -= <<= >>= >>>= &= ^= |=

Various assignment operators