Groovy-assignment-operators

提供:Dev Guides
2020年6月23日 (火) 09:26時点におけるMaintenance script (トーク | 投稿記録)による版 (Imported from text file)
(差分) ← 古い版 | 最新版 (差分) | 新しい版 → (差分)
移動先:案内検索

Groovy-割り当て演算子

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

出力は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になります

class Example {
   static void main(String[] args) {
      int x = 5;

      println(x+=3);
      println(x-=3);
      println(x*=3);
      println(x/=3);
      println(x%=3);
   }
}

上記のプログラムを実行すると、次の結果が得られます。 上記のように、結果は演算子の説明から予想されるとおりであることがわかります。

8
5
15
5
2