Tutorial

What is += Addition Assignment Operator in Java?

Published on August 3, 2022
Default avatar

By Jayant Verma

What is += Addition Assignment Operator in Java?

While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

It’s the Addition assignment operator. Let’s understand the += operator in Java and learn to use it for our day to day programming.

x += y in Java is the same as x = x + y.

It is a compound assignment operator. Most commonly used for incrementing the value of a variable since x++ only increments the value by one.

Incrementing Values With the += Operator

This code will increase the value of a by 2. Let’s see the examples:

int a = 1;
a+=2;
System.out.println(a);
output
Output

On the other hand if we use a++:

int a = 1;
a++;
System.out.println(a);
Output 2 1
Output

The value of a is increased by just 1.

Using += in Java Loops

The += operator can also be used with for loop:

for(int i=0;i<10;i+=2)
{
    System.out.println(i);
}
For Loop Output 1
Output

The value of i is incremented by 2 at each iteration.

Working with multiple data types

Another interesting thing to note is that adding int to double using the regular addition expression would give an error in Java.

int a = 1;
a = a + 1.1; // Gives error 
a += 1.1;
System.out.println(a);

The first line here gives an error as int can’t be added to a double.

Output:

error: incompatible types: possible lossy conversion from double to int
a = a + 1.1; // Gives error 

However, when using the += operator in Java, the addition works fine as Java now converts the double to an integer value and adds it as 1. Here’s the output when the code is run with only the += operator addition.

Output
Output

E1 op= E2 is equivalent to E1 = (T) ((E1) op (E2)), where T is the type of E1, except that E1 is evaluated only once. This is Java doing typecasting to add the two numbers.

String Concatenation

The += operator also works for string mutation.

String a = "Hello";
a+="World";
System.out.println(a);
String mutation Output
Output

The string “Hello” has been mutated and the string “World” has been concatenated to it.

Conclusion

The += is an important assignment operator. It is most commonly used with loops. The same assignment also works with other operators like -=, *=, /=.

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about us


About the authors
Default avatar
Jayant Verma

author

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel