- Groovy Tutorial
- Groovy - Home
- Groovy - Overview
- Groovy - Environment
- Groovy - Basic Syntax
- Groovy - Data Types
- Groovy - Variables
- Groovy - Operators
- Groovy - Loops
- Groovy - Decision Making
- Groovy - Methods
- Groovy - File I/O
- Groovy - Optionals
- Groovy - Numbers
- Groovy - Strings
- Groovy - Ranges
- Groovy - Lists
- Groovy - Maps
- Groovy - Dates & Times
- Groovy - Regular Expressions
- Groovy - Exception Handling
- Groovy - Object Oriented
- Groovy - Generics
- Groovy - Traits
- Groovy - Closures
- Groovy - Annotations
- Groovy - XML
- Groovy - JMX
- Groovy - JSON
- Groovy - DSLS
- Groovy - Database
- Groovy - Builders
- Groovy - Command Line
- Groovy - Unit Testing
- Groovy - Template Engines
- Groovy - Meta Object Programming
- Groovy Useful Resources
- Groovy - Quick Guide
- Groovy - Useful Resources
- Groovy - Discussion
Groovy - Assignment Operators
The Groovy language also provides assignment operators. Following are the assignment operators available in 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 |
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); } }
When we run the above program, we will get the following result. It can be seen that the results are as expected from the description of the operators as shown above.
8 5 15 5 2
groovy_operators.htm
Advertisements