what does += mean in java

In this post, we will see what does += mean in java.

Java += operator

+= is compound addition assignment operator which adds value of right operand to variable and assign the result to variable. Types of two operands determine the behavior of += in java.

In the case of number, += is used for addition and concatenation is done in case of String.

a+=b is similar to a=a+b with one difference which we will discuss later in the article.

Let’s see with the help of example:

Output: // Prints 6


Using += in loops

You can use += in for loop when you want to increment value of variable by 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.

Let’s understand with the help of example:
If you want to print even number from 0 to 10, then you could use += operator as below:


0 2 4 6 8 10

Difference between a+=b and a=a+b

If a and b are of different types, the behavior of a+=b and a=a+b will differ due to rule of java language.

Let’s understand with the help of example:

Here, += does implicit cast, where as + operator requires explicit cast for second operand, otherwise it won’t compile.

As per oracle docs for compound assignment expression:

A compound assignment expression of the form 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.


is equivalent to:

Using += for String concatenation

You can use += operator for String concatenation as well.



That’s all about what does += mean in java.

Was this post helpful?

Related Posts

  • Question mark in java
    20 May

    Question mark operator in java

    Learn about Question mark operator in java. It is also know as ternary operator.

  • 29 March

    Modulo operator in java

    Table of ContentsModulo operator SyntaxModulo operator usagesEven odd programPrime number programModulo operator behaviour with negative integer In this post, we will see about modulo or modulus operator in java. Modulo operator(%) is used to find the remainder when one integer is divided by another integer. Modulo operator Syntax [crayon-632f726ec038d681203260/] We can’t use modulo with any […]

  • 22 March

    XOR operator in java

    In this tutorial, we will see about XOR operator in java. XOR operator or exclusive OR takes two boolean operands and returns true if two boolean operands are different. XOR operator can be used when both the boolean conditions can’t be true simultaneously. Here is truth table for XOR operator. Let’s say you have Person […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.