Java Addition Assignment
In Java, Addition Assignment Operator is used to add a value (right operand) to this variable (left operand) and assign the result back to this variable (left operand). In this tutorial, we will learn how to use Addition Assignment operator in Java, with examples.
The syntax to add a value of 2
to variable x
and assign the result to x
using Addition Assignment Operator is
</>
Copy
x += 2
Example
In the following example, we take a variable x
with an initial value of 5
, add a value of 2
to x
and assign the result to x
, using Addition Assignment Operator.
Main.java
</>
Copy
public class Main {
public static void main(String[] args) {
int x = 5;
//addition assignment
x += 2;
System.out.println("x : " + x);
}
}
Output
x : 7
Conclusion
In this Java Tutorial, we learned about Addition Assignment Operator in Java, with examples.