The operators carry out particular actions on one or more operands. There are several distinct kinds of operators available in Java. The arithmetic operators, which include addition, subtraction, multiplication, and division operators, are the most widely used ones. Additionally, there are operators for assigning values to variables, like the assignment = operator.
In this tutorial, the concept of the Java addition assignment += operator will be discussed.
What is the Java Addition Assignment += Operator?
The addition assignment += operator is a significant feature in the Java programming language. It gives a concise way to perform addition and assignment in a single step. This operator is primarily utilized to update the value of a variable by adding a given value to its current value.
The += operator combines the addition and assignment operators. This operator takes the form of variable += expression, where the variable is the target variable to be updated, and the expression is the value to be added to the variable. The += operator is applied to numerical data types such as integers, floats, and even characters.
Syntax
variable += expression;
In this syntax, “expression” refers to the value that will be added to the “variable”. The expression can be any valid value or arithmetic operation that evaluates to the desired result.
For example, if we have a variable x and want to increment its value by 5, we can simply write x += 5. This line of code is equivalent to x = x + 5, but the former one is more concise and elegant.
Examples of Using += Operator in Java
Let’s look at some code examples to see how the “+=” operator is used in various scenarios:
Example 1: Incrementing the Integer
This example utilizes the += operator to increment the defined integer:
package com.company;
public class Class1{
public static void main(String[] args) {
int num = 3;
num += 6;
System.out.println(num);
}
}
In this example, the “+=” operator adds 6 to the variable “num” and assigns the result back to “num“. This operation is equivalent to explicitly writing “num = num + 6“.
Output

Example 2: Concatenating the String
The += operator is not limited to just numerical addition. It can also be applied to other types of variables, such as strings. When applied to string concatenation, the += operator enables the easy merging of two strings, thereby simplifying the code, as follows:
package com.company;
public class Class1{
public static void main(String[] args) {
String msg = "My name is, ";
String name = "Alex";
msg += name;
System.out.println(msg);
}
}
According to this block of code, the “+=” operator concatenates the value of “name” to the “msg” variable, resulting in the final string as “My name is, Alex“.
Output

Example 3: Applying the Compound Operations
Moreover, the += operator can be combined with other operators, such as subtraction (-), multiplication (*), division (/), and modulo (%), providing a range of possibilities for efficient and concise calculations. These compound operators enable developers to perform calculations and assignments simultaneously, minimizing the number of lines and improving code efficiency.
Following is the code demonstration:
package com.company;
public class Class1{
public static void main(String[] args) {
int res= 5;
res+= 5 / 3;
System.out.println(res);
}
}
In these lines of code, the “+=” operator performs a compound operation, adding the result of the expression “5 / 3” to the “res” variable. The code then prints the result on the screen.
Output

Example 4: Adding the Defined Integers
The += operator is not limited to working with constants or literals. It can also be used with variables on both sides of the operator. This flexibility allows for more dynamic and adaptable code, as the values involved can be determined at runtime or based on various conditions:
package com.company;
public class Class1{
public static void main(String[] args) {
int num = 32;
int num1 = 13;
num += num1;
System.out.println(num);
}
}
In the above code, the “+=” operator adds the value of “num1” to “num” and stores the result in “num“. The result is then returned.
Output

Example 5: Utilization in “for” Loops
You can also use += in the “for” loop when you want to increment the value of a variable by “1” or more than 1. In general, you might have used “i++”, but if you want to increment it by 2, then you can use “i+=2” and so on:
package com.company;
public class Class1{
public static void main(String[] args) {
for (int x = 0; x <= 6; x += 3) {
System.out.print(x + " ");
}
}
}
In this snippet of code, the “+=” operator is used to iterate the loop with an increment of 3 in x. The value of x is then printed on the screen.
Output

Precedence of the Addition Assignment += Operator
In terms of precedence, the += operator has the “same priority” as the regular assignment operator (=). This means that it is evaluated after other arithmetic operators such as multiplication and division but before the assignment. Therefore, it is crucial to understand the order in which operators are evaluated to avoid any unexpected outcomes.
Conclusion
The addition assignment += operator in Java is a powerful tool that combines addition and assignment in a single step. Its versatility extends beyond numerical addition, enabling the concatenation of strings and the manipulation of compound data structures. Moreover, the += operator can be used in conjunction with other operators, ensuring efficient and elegant code implementation.