Addition Assignment Operator mean in Java
The += operator in Java is known as the Addition assignment operator and we will now comprehend its usage in daily programming tasks.
In Java, the statement x += y can be equivalently written as x = x + y.
The compound assignment operator is frequently employed for incrementing a variable, as the x++ operator only increases the value by one.
Increasing values using the += Addition assignment operator.
This code will add 2 to the value of a. Here are some examples:
int a = 1;
a+=2;
System.out.println(a);
![output](https://cdn.silicloud.com/blog-img/blog/img/655dd62dcdcf9b6757a01ca0/6-0.png)
Alternatively, if we utilize a++,
int a = 1;
a++;
System.out.println(a);
![Output 2 1](https://cdn.silicloud.com/blog-img/blog/img/655dd62dcdcf9b6757a01ca0/9-0.png)
The value of a is simply incremented by 1.
Utilizing the “plus equal” operator in Java loops.
The for loop also allows the use of the += operator.
for(int i=0;i<10;i+=2)
{
System.out.println(i);
}
![For Loop Output 1](https://cdn.silicloud.com/blog-img/blog/img/655dd62dcdcf9b6757a01ca0/14-0.png)
At each iteration, the value of i is increased by 2.
Engaging in tasks involving a variety of data types.
In Java, it is worth mentioning that attempting to add an int to a double using the conventional addition expression would result in an error.
int a = 1;
a = a + 1.1; // Gives error
a += 1.1;
System.out.println(a);
An error occurs in the first line because it is not possible to add an int to a double.
Can you provide one paraphrase option for the following sentence:
“At a museum, there were many interesting paintings on display.”
Paraphrase Option: “The museum had numerous captivating paintings exhibited.”
error: incompatible types: possible lossy conversion from double to int
a = a + 1.1; // Gives error
However, when Java utilizes the += operator, the addition functions correctly as it converts the double to an integer value and adds it as 1. Here is the result obtained when the code is executed with only the += operator addition.
![Output](https://cdn.silicloud.com/blog-img/blog/img/655dd62dcdcf9b6757a01ca0/23-0.png)
In Java, when you use the expression E1 op= E2, it is essentially the same as E1 = (T) ((E1) op (E2)), with T representing the type of E1. The only difference is that the evaluation of E1 is only performed once. This is how Java handles typecasting to combine the two numbers.
Combining strings
The += operator can be used to mutate strings as well.
String a = "Hello";
a+="World";
System.out.println(a);
![String mutation Output](https://cdn.silicloud.com/blog-img/blog/img/655dd62dcdcf9b6757a01ca0/28-0.png)
The string “Hello” has been altered and the string “World” has been added to it.
In summary, to conclude
The += Addition assignment operator holds significant importance and is predominantly employed in loops. Similarly, this assignment can also be used with other operators such as -=, *=, and /=.
More Java tutor:
convert string to character array in Java.(Opens in a new browser tab)