Java Language Reference

Previous Chapter 4
Expressions
Next
 

4.6 Additive Operators

The additive operators in Java are binary operators that are used for addition and string concatenation (+) and subtraction (-). The additive operators appear in additive expressions.

[Graphic: Figure from the text]

The additive operators are equal in precedence and are evaluated from left to right.

References Multiplicative Operators; Order of Operations

Arithmetic Addition Operator +

The binary arithmetic addition operator + produces a pure value that is the sum of its operands. The arithmetic + operator may appear in an additive expression. The arithmetic addition operator never throws an exception.

Here is an example that uses the arithmetic addition operator:

int addThree (int x, int y, int z) {
    return x + y + z;
}

If the type of either operand of + is a reference to a String object, the operator is the string concatenation operator, not the arithmetic addition operator. The string concatenation operator is described in String Concatenation Operator +.

Otherwise, the types of both operands of the arithmetic addition operator must be arithmetic types, or a compile-time error occurs. The + operator may perform type conversions on its operands:

If the addition of integer data overflows, the value produced by + contains the low order bits of the sum and the sign of the value is the opposite of the mathematically correct sign, due to the limitations of the two's complement representation used for integer data.

The addition of floating-point data is governed by the following rules:

References Arithmetic Types; String Concatenation Operator +

Arithmetic Subtraction Operator -

The binary subtraction operator - produces a pure value that is the difference between its operands; it subtracts its right operand from its left operand. The arithmetic - operator may appear in an additive expression. The arithmetic subtraction operator never throws an exception.

Here is an example that uses the arithmetic subtraction operator:

int subThree (int x, int y, int z) {
    return x - y - z;
}

The types of both operands of the arithmetic subtraction operator must be arithmetic types, or a compile-time error occurs. The - operator may perform type conversions on its operands:

For both integer and floating-point data, a-b always produces the same result as a-(+b).

If the subtraction of integer data overflows, the value produced by - contains the low order bits of the difference and the sign of the value is the opposite of the mathematically correct sign, due to the limitations of the two's complement representation used for integer data.

The subtraction of floating-point data is governed by the following rules:

References Arithmetic Types

String Concatenation Operator +

The string concatenation operator + produces a pure value that is a reference to a String object that it creates. The String object contains the concatenation of the operands; the characters of the left operand precede the characters of the right operand in the newly created string. The string concatenation + operator may appear in an additive expression.

Here is an example of some code that uses the string concatenation operator:

// format seconds into hours, minutes, and seconds
String formatTime(int t) {
    int minutes, seconds;
    seconds = t%60;
    t /= 60;
    minutes = t%60;
    return t/60 + ":" + minutes + ":" + seconds;
}

If neither operand of + is a reference to a String object, the operator is the arithmetic addition operator, not the string concatenation operator. Note that Java does not allow a program to define overloaded operators. However, the language defines the + operator to have a meaning that is fundamentally different from arithmetic addition if at least one of its operands is a String object.

The way in which Java decides if + means arithmetic addition or string concatenation means that the use of parentheses can alter the meaning of the + operator. Consider the following code:

int x = 3, y = 4;
System.out.println("x = " + x + ", y = " + y);
System.out.println("\"??\" + x + y ==> " + "??" + x + y);
System.out.println("\"??\" + (x + y) ==> " + "??"+ (x + y));

In the output from this code, you can see that the addition of parentheses changes the meaning of the last + from string concatenation to arithmetic addition:

x = 3, y = 4
"??" + x + y ==> ??34
"??" + (x + y) ==> ??7

If one of the operands of + is a String object and the other is not, the operand that is not a String object is converted to one using the following rules:

Java uses the StringBuffer object to implement string concatenation. Consider the following code:

String s, s1,s2;
s = s1 + s2;

To compute the string concatenation, Java compiler generates code equivalent to:

s = new StringBuffer().append(s1).append(s2).toString();

Consider another expression:

s = 1 + s1 + 2

In this case, the Java compiler generates code equivalent to:

s = new StringBuffer().append(1).append(s1).append(2).toString()

No matter how many strings are being concatenated in an expression, the expression always produces exactly one StringBuffer object and one String object.[3] From an efficiency standpoint, if you concatenate more than two strings, it may be more efficient to do so in a single expression, rather than in multiple expressions.

[3] Although an optimizing compiler should be smart enough to combine multiple concatenation expressions when it is advantageous, the compiler provided with Sun's reference implementation of Java does not do this.

References Arithmetic Addition Operator +; Object; String; StringBuffer


Previous Home Next
Multiplicative Operators Book Index Shift Operators

Java in a Nutshell Java Language Reference Java AWT Java Fundamental Classes Exploring Java