Tutorials  /  JavaScript

What is += Addition Assignment Operator in Java?

Ccentron Redaktion · January 2024 ·3 min read ·JavaScript, 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:

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

On the other hand if we use a++:

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

The value of a is increased by just 1.

VM

Matching infrastructure at centron

Node applications need somewhere to run: ccloud³ VMs with full root access from €3.12 per month – or as a managed server if you would rather not run it yourself. Rent a cloud server →

Using += in Java Loops

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

Code
for(int i=0; i<10; i+=2)
{
    System.out.println(i);
}

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.

Code
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. 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.

String Concatenation

The += operator also works for string mutation.

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

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 -=, *=, /=.

Jetzt 200 € Guthaben sichern

Testen Sie Ihr Setup auf ccloud³

Registrieren Sie sich in der ccloud³ und erhalten Sie 200 € Startguthaben für Ihr Projekt – z. B. für eine PostgreSQL-VM mit automatischen Backups.

centron Redaktion Technische Redaktion

Das Redaktionsteam von centron schreibt Anleitungen aus dem Betriebsalltag: getestet auf unserer eigenen Plattform, betrieben im Rechenzentrum in Hallstadt bei Bamberg.

Kategorie JavaScript
Teilen
Noch offene Fragen?

Unser Team hilft Ihnen bei Ihrem konkreten Setup weiter – von Menschen, die die Plattform selbst betreiben.

War dieses Tutorial hilfreich?

Ihre Antwort wird anonym gespeichert und hilft uns, die Tutorials zu verbessern.

Kommentare

Noch keine Kommentare – stellen Sie die erste Frage zu diesem Tutorial.

Zum Kommentieren anmelden

Kommentare stehen centron-Kunden offen. Melden Sie sich in Ihrem Konto an, um eine Frage zu diesem Tutorial zu stellen.

Weiterlesen

Das könnte Sie auch interessieren

Jetzt kostenlos anfangen

Melden Sie sich an und erhalten Sie in den ersten 60 Tagen ein Guthaben von 200 € bei centron.

Dieses Werbeangebot gilt nur für neue Konten. Angebot ausschließlich für Gewerbetreibende.

Jetzt loslegen Sales kontaktieren