Groovy-operators
Groovy-オペレーター
演算子は、特定の数学的または論理的な操作を実行するようコンパイラーに指示する記号です。
Groovyには次の種類の演算子があります-
- 算術演算子
- 関係演算子
- 論理演算子
- ビットごとの演算子
- 割り当て演算子
算術演算子
Groovy言語は、すべての言語として通常の算術演算子をサポートしています。 以下はGroovyで利用可能な算術演算子です-
link:/groovy/groovy_arithmetic_operators [例を表示]
Operator | Description | Example |
---|---|---|
PLUS | Addition of two operands | 1 PLUS 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 |
PLUSPLUS | Incremental operators used to increment the value of an operand by 1 |
int x = 5;
|
— | Incremental operators used to decrement the value of an operand by 1 |
int x = 5; x--; xは4を与える |
関係演算子
関係演算子は、オブジェクトの比較を許可します。 以下はGroovyで利用可能な関係演算子です-
リンク:/groovy/groovy_relational_operators [例を表示]
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で利用可能な論理演算子です-
リンク:/groovy/groovy_logical_operators [例を表示]
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 |
ビット演算子
Groovyは4つのビット演算子を提供します。 以下はGroovyで利用可能なビットごとの演算子です-
リンク:/groovy/groovy_bitwise_operators [例を表示]
Sr.No | Operator & Description |
---|---|
1 |
& これはビット単位の「and」演算子です |
2 | * |
これはビット単位の「or」演算子です |
3 |
^ これはビット単位の「xor」または排他的論理和演算子です |
4 |
これらの演算子を示す真理値表を次に示します。
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 |
割り当て演算子
Groovy言語には、代入演算子も用意されています。 以下はGroovyで利用可能な代入演算子です-
リンク:/groovy/groovy_assignment_operators [例を表示]
Operator | Description | Example |
---|---|---|
+= | This adds right operand to the left operand and assigns the result to left operand. |
def A = 5 A+=3 出力は8になります |
-= | This subtracts right operand from the left operand and assigns the result to left operand |
def A = 5 A-=3 出力は2になります |
*= | This multiplies right operand with the left operand and assigns the result to left operand |
def A = 5 A*=3 出力は15になります |
/= | This divides left operand with the right operand and assigns the result to left operand |
def A = 6 A/=3 出力は2になります |
%= | This takes modulus using two operands and assigns the result to left operand |
def A = 5 A%=3 出力は2になります |
範囲演算子
Groovyは範囲の概念をサポートし、..の助けを借りて範囲演算子の表記を提供します。 表記法 範囲演算子の簡単な例を以下に示します。
def range = 0..5
これは単純な整数の範囲を定義するもので、下限が0、上限が5のrangeというローカル変数に格納されます。
次のコードスニペットは、さまざまな演算子の使用方法を示しています。
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
演算子の優先順位
次の表に、すべてのgroovy演算子を優先順位順に示します。
Sr.No | Operators & Names |
---|---|
1 |
++ — + - 事前インクリメント/デクリメント、単項プラス、単項マイナス |
2 |
乗算、div、モジュロ |
3 |
加算、減算 |
4 |
== != <⇒ 等しい、等しくない、と比較 |
5 |
& バイナリ/ビット単位 |
6 |
^ バイナリ/ビットごとのxor |
7 | * |
バイナリ/ビット単位または |
8 |
&& 論理的 |
9 |
* | |
論理的または |
10 |
= *= *=/= %= += -= <⇐ >>= >>>= &= ^= |
=* さまざまな割り当て演算子 |