Java

What Does += Mean in Java?

In Java, there are some operators that can be used to assign values to the variables known as assignment operators. Among them, the most commonly used assignment operators are “=”, “+=”, “-=” etc. In this write-up, we are going to cover the different aspects of the “+=” operator which is referred as an “addition assignment operator”. The “+=” operator allows us to perform addition and assignment in one step.

In this write-up, we are going to explore the following concepts:

So let’s get started!

What does += mean in Java

It is a shorthand assignment operator commonly referred to as “compound addition assignment operator”. The += operator performs two functionalities in one go i.e. firstly, it performs the addition on the operands, and then it assigns the result to the left operand.

In simple words, we can say that the variable1 += variable2 has the same meaning as variable1 = variable1 + variable2.

Behavior of += Operator With Respect to Data Types

The behavior of the += operator depends on the data type of operands i.e. if the operands are strings then it is used for the concatenation purpose and if the operands are numbers then it is used for the numbers.

How to Use += to Increment a Value

In java, the ++ operator increments a value by 1 however, using the addition assignment operator we can specify an increment of our choice.

Example

Let’s consider we have a variable “number” that holds a value 50, now if we have to increment it by 5 then we can do it in this way:

public class AssignmentOperatorExample {
  public static void main(String[] args) {
        int number = 50;
        number +=5;
        System.out.println(number);
   }
 }
}

The complete code snippet and the respective output is shown in the below snippet:

From the above-given output, we can see that the number is incremented by 5.

How to Use += in Loops

The addition assignment operator can be used in the java looping structures to increment a value by more than one.

Example

If we have to print the table of “5” then we can utilize the for loop and within the loop, we can increment the value by five times in each iteration:\

public class AssignmentOperatorExample {
    public static void main(String[] args) {
        for(inti=5; i<=50; i+=5 )
        {
            System.out.println(i);
        }
    }
}

In the above code snippet, we initialize the loop from “5” and specify the termination criteria as “i<=50”. Next, we utilize the “+=” operator which will increment the value by 5 in each iteration. And in this way, 10 iterations will be performed until the value of “i” satisfy the termination condition i.e. “i<=50”:

The above snippet verifies the working of += operator.

How to Use += for String Concatenation

The += operator can be used to concatenate the strings

Example

Let’s consider the below-given code snippet for a profound understanding of how to concatenate the strings using += operator in java:

public class AssignmentOperatorExample {
    public static void main(String[] args) {
        String str = "Linux";
        str += "Hint";
        System.out.println(str);
}
}

The original string is “Linux” and we concatenate the “Hint” with it using the += operator:

The output verifies that the strings are concatenated successfully.

Conclusion

In java, the += operator is used to perform two functionalities in one go i.e. firstly it performs addition then assignment. Using += operator either addition or concatenation can be performed depending upon the data type of operands. Moreover, the += operator can be used as the increment operator in java loops.

This write-up provides a comprehensive overview of the += operator where we have learned different use-cases of the java += operator.

About the author

Anees Asghar

I am a self-motivated IT professional having more than one year of industry experience in technical writing. I am passionate about writing on the topics related to web development.