Read Also: String Concatenation in Java
It is also known as a compound assignment operator.
What does += mean in Java with examples
There are many ways in which we can use += in Java.1. Incrementing int values with += operator
The below code will increase the value of x by 5.
int x = 10;
x += 5;
System.out.println(x); // Print 15
On the other hand, if we use x++ in the code, it will increment the value of x by 1 as shown below in the example.
int x = 15;
x++;
System.out.println(x); // Print 16
2. Using += operator in for loop
You can also use the += operator(addition assignment operator) in Java to increment the value of the variable by more than 1.
For example, in the below for loop, we are printing the even numbers from 0 to 15 using the += operator. The value of variable i is incremented by 2 at each iteration of the for loop.
public class PrintEvenNumber
{
public static void main(String args[])
{
for (int i=0; i < 15; i+=2)
{
System.out.print(" "+ i + " ");
}
}
}
Output:
0 2 4 6 8 10 12 14
3. Multiple Data types
Data types of two operands determine the behavior of this operator. For example, if the data type of both operands is a number then += will be used for addition. On the other hand, if the datatype of both operands is String then it is used for concatenation.
a. Difference between a=a+b and a+=b in Java
Things get interesting when we have a and b of different data types.
1. Data type of variable a as int and b as double
If we add an int to double using the regular addition expression, we will get an error as shown below in the example:
public class AdditionAssignmentOperator
{
public static void main(String args[])
{
int x =1;
x += 2.2; // Print 3, will cast 2.2 to 2 internally
x = x + 2.2; // Throws compile time error, can not convert from double to int
System.out.println(x);
}
}
Output:
/AdditionAssignmentOperator.java:7: error: incompatible types: possible lossy conversion from double to int
x = x + 2.2; // Throws compile time error, can not convert from double to int
^
1 error
💡 Did You Know?
According to Oracle docs, a compound statement expression of the form x op= y is equivalent to x = (T) ((x) op (y)) where T is of type x, except that x is evaluated only once.
According to Oracle docs, a compound statement expression of the form x op= y is equivalent to x = (T) ((x) op (y)) where T is of type x, except that x is evaluated only once.
Let's understand the above line with the help of an example:
The following code is correct:
int a = 13;
a += 7.03;
The above code will result in a value of 20 because it is equivalent to
int a = 13;
a = (int) (13 + 7.03);
4. += for String Concatenation
You can use the += addition assignment operator for String concatenation as well, as shown below in the example:
public class AdditionAssignmentOperator2
{
public static void main(String args[])
{
String x = "Alive is";
x += "Awesome";
System.out.println(x);
}
}
Output:
Alive is Awesome
That's all for today. Please mention in the comments if you have any questions related to what does += mean in Java with examples.