Groovy-arithmetic-operators
提供:Dev Guides
Groovy-算術演算子
Groovy言語は、すべての言語として通常の算術演算子をサポートしています。 以下はGroovyで利用可能な算術演算子です-
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を与える |
次のコードスニペットは、さまざまな演算子の使用方法を示しています。
class Example {
static void main(String[] args) {
//Initializing 3 variables
def x = 5;
def y = 10;
def z = 8;
//Performing addition of 2 operands
println(x+y);
//Subtracts second operand from the first
println(x-y);
//Multiplication of both operands
println(x*y);
//Division of numerator by denominator
println(z/x);
//Modulus Operator and remainder of after an integer/float division
println(z%x);
//Incremental operator
println(x++);
//Decrementing operator
println(x--);
}
}
上記のプログラムを実行すると、次の結果が得られます。 上記のように、結果は演算子の説明から予想されるとおりであることがわかります。
15
-5
50
1.6
3
5
6