Table of content

What does += mean in Java?

Assignment operator for integer data type

The += is an addition assignment operator. It adds the value of the right operand to the variable and assigns the result to the variable. It is equivalent to i= i+n.

Let us consider a variable i. The operation i+=n is equal to i=i+n, where n is a constant value and the value of i varies.

``````int i=2;
int n=3;
i+=n; // it is equivalent to i=i+n
// output
i=5
``````

The operations i++ and i+=1 do the same task of incrementing the value of i by 1. In fact, i+=1 is a shorthand notation of i++.

The difference between i++ and i+= is that i++ is restricted to increment the value of i by 1, whereas i+= can be used to increment i value by any numeric value like i+=2, i+=200, i+=300,... and so on.

In the operation i+=n the data types of n and i should be the same.

The below example illustrates the use of += operator. In the program, initially the value of a=4. After the operation a+=5 evaluates a=a+5=4+5, hence value of a = 9.

``````import java.util.*;
public class Main {
public static void main(String[] args) {
int n = 4, a = 10;
a += n;
System.out.println("Value of a = " + a);
}
}``````

The output is

``Value of a = 14``

Assignment operator for String data type

In Strings, the operator += can be used to concatenate two strings.

The example below illustrates the concatenation of strings using += operator.

``````import java.io.*;
public class HelloWorld {
public static void main(String[] args) {
String txt = "Welcome to the world ";
txt += "of Java";
System.out.println(txt);
}
}``````

The output is as follows:

``Welcome to the world of Java``

Assignment operator for float data type

The float data type is when the user needs to carry some operations with decimal numbers. The addition assignment operator can be used to add a floating-point value to a variable. In the operation i+=x.y, x.y is the floating-point value and remains constant.

The example below illustrates the operation of the += operator over floating-point values. Here b = 8.2 is added to c in each iteration of for loop.

``````import java.util.*;
public class floatop {
public static void main(String args[]) {
float b = 8.2 f, c = 1.8 f;
c += b;
System.out.println(c);
}
}``````

The output is

``10.0``

Assignment operator for double data type

The double data type is generally used for decimal values just like float. The assignment operator can be used to add two double numbers. The operation of the operator i+=m.nx is the same as in float data type. The value of m.nx remains constant while the value of i varies.

Let us consider an example of adding a double data type.

``````import java.util.*;
public class doubop {
public static void main(String args[]) {
double m = 100.005, n = 500.005;
n += m;
System.out.println("Result=" + n);
}
}``````

Output

``````Result=600.01
``````

Assignment operator for short data type

The short data type can be used to save memory. The addition assignment operator can also be used in the addition operation of short data types. Let us consider k+=n, where k and n should be short data types and n is a constant short value.

Example program

``````import java.util.*;
public class shortop {
public static void main(String args[]) {
short k = 10000, n = 500;
k += n;
System.out.println("Value of k=" + k);
}
}``````

The output is

``Value of k=10500``

Assignment operator for long data type

The long data type is used when the user requires a range of values that is more than those provided by int. The assignment operator can be used for the operations of long data types. In the addition assignment operator p+=q, where p and q are both long data types, p and q should be a long data type variable.

An example illustrates the use of assignment operator with long data types

``````import java.util.*;
public class shortop {
public static void main(String args[]) {
long p = 50000, q = 1000;
p += q;
System.out.println("Value of p=" + p);
}
}``````

The output is

``Value of p=51000``
0 results
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions