Java

Java += and -= Operators

In Java,

a = a + b;

means the old value of ‘a’ is added to the value of b, and the result of this sum becomes the new value of ‘a’. This statement can be rewritten as:

a += b;

+= is the addition assignment operator. + is on the left, while = is on the right. If + is on the right side, then it would mean plus b (operand).

Still in Java,

a = a - b;

means, b is subtracted from the old value of ‘a’, and the result of this difference becomes the new value of ‘a’. This statement can be rewritten as:

a -= b;

-= is the subtraction assignment operator. – is on the left, while = is on the right. If – is on the right side, then it would mean minus b (operand).

This article illustrates the use of the += and the -= operators with Java numeric types. It also illustrates the use of the += operator with the string type.

Article Content

Numeric Types

Integer

Addition Assignment

Consider the following code segment:

int a = 5;

int b = 3;

a += b;

System.out.println(a);

‘a’ and b are integer variables holding different numbers. The output is 8, meaning the old value of ‘a’ was added to the value of b to give the new value of ‘a’.

Subtraction Assignment

Consider the following code segment:

int a = 5;

int b = 3;

a -= b;

System.out.println(a);

‘a’ and b are integer variables holding different numbers. The output is 2, meaning the value of b was subtracted from the old value of ‘a’ to give the new value of ‘a’.

Float

Addition Assignment

Consider the following code segment:

float a = 5.5f;

float b = 3.5f;

a += b;

System.out.println(a);

‘a’ and b are float variables holding different numbers. In Java, a float number is distinguished from a double number by ‘f’ at its end. The output is 9.0, meaning the old value of ‘a’ was added to the value of b to give the new value of ‘a’.

Subtraction Assignment

Consider the following code segment:

float a = 5.5f;

float b = 3.5f;

a -= b;

System.out.println(a);

‘a’ and b are float variables holding different numbers. In Java, a float number is distinguished from a double number by ‘f’ at its end. The output is 2.0, meaning the value of b was subtracted from the old value of ‘a’ to give the new value of ‘a’.

Double

Addition Assignment

Consider the following code segment:

double a = 5.5;

double b = 3.5;

a += b;

System.out.println(a);

‘a’ and b are double variables holding different numbers. In Java, a float number is distinguished from a double number by ‘f’ at its end. A double number does not have ‘f’ or ‘d’ at its end. The output is 9.0, meaning the old value of ‘a’ was added to the value of b to give the new value of ‘a’.

Subtraction Assignment

Consider the following code segment:

double a = 5.5;

double b = 3.5;

a -= b;

System.out.println(a);

‘a’ and b are float variables holding different numbers. In Java, a float number is distinguished from a double number by ‘f’ at its end. The output is 2.0, meaning the value of b was subtracted from the old value of ‘a’ to give the new value of ‘a’.

byte

Addition Assignment

Consider the following code segment:

byte a = 5;

byte b = 3;

a += b;

System.out.println(a);

‘a’ and b are byte variables holding different numbers. The output is 8, meaning the old value of ‘a’ was added to the value of b to give the new value of ‘a’.

Subtraction Assignment

Consider the following code segment:

byte a = 5;

byte b = 3;

a -= b;

System.out.println(a);

‘a’ and b are byte variables holding different numbers. The output is 2, meaning the value of b was subtracted from the old value of ‘a’ to give the new value of ‘a’.

String Class

+ and Strings

With strings, + means that two strings should be concatenated. Consider the following code segment:

String c = "Tough" + " Guy";

System.out.println(c);

The output is “Tough Guy”. So the two strings have been concatenated.

Java has += for strings, but it does not have -= for strings.

With Java strings,

a = a + b;

means the old string literal value of ‘a’ is concatenated in front of the literal string value of b, and the result of this concatenation becomes the new value of ‘a’. This statement can be rewritten as:

a += b;

+= is the addition assignment operator. + is on the left, while = is on the right.

Consider the following code segment:

String a = "Tough";

String b = " Guy";

a += b;

System.out.println(a);

‘a’ and b are string variables holding different literals. The output is “Tough Guy”, meaning the old value of ‘a’ was concatenated in front of the value of b to give the new value of ‘a’.

This can still be done with b as a string literal, as in the following code segment:

String a = "Tough";

a += " Guy";

System.out.println(a);

The output is still “Tough Guy”.

char

+= and -= works with chars, of the idea for example, that ‘B’ + 1 = ‘C’ and ‘B’ – 1 = ‘A’.

Addition Assignment

Consider the following code segment:

char a = 'B';

int b = 1;

a += b;

System.out.println(a);

‘a’ is a char variable, and b is an integer variable; they hold different “numbers”. The output is ‘C’, meaning the old value of ‘a’ was added to the value of b to give the new value of ‘a’.

Subtraction Assignment

Consider the following code segment:

char a = 'B';

int b = 1;

a -= b;

System.out.println(a);

‘a’ is a char variable, and b is an integer variable; they hold different ‘numbers”. The output is ‘A’, meaning the value of b was subtracted from the old value of ‘a’ to give the new value of ‘a’.

Conclusion

The statement,

a += b;

means the old value of ‘a’ is added to the value of b, and the result of this sum becomes the new value of ‘a’. The statement,

a -= b;

means, b is subtracted from the old value of ‘a’, and the result of this difference becomes the new value of ‘a’.

The string class has += but does not have -= ; and it is for concatenation.

About the author

Chrysanthus Forcha

Discoverer of mathematics Integration from First Principles and related series. Master’s Degree in Technical Education, specializing in Electronics and Computer Software. BSc Electronics. I also have knowledge and experience at the Master’s level in Computing and Telecommunications. Out of 20,000 writers, I was the 37th best writer at devarticles.com. I have been working in these fields for more than 10 years.