SCJP Study Guide:
Fundamentals


Printer-friendly version Printer-friendly version | Send this 
article to a friend Mail this to a friend


Previous Next vertical dots separating previous/next from contents/index/pdf Contents
XyzWs SCJP Study Guide: Arithmetical Operators

Arithmetical Operators

Arithmetic operators refer to the standard mathematical operators you learned in school: addition +, subtraction -, multiplication *, division /, and remainder %.

A literal is a numeric value that is assigned to a variable like 2 or 5.8. Floating point numbers can be represented in the scientific notation 5.43e+2 which is 543. These operators can be applied to all integers and floating point numbers. Integer division truncates the result. There are two symbols used to handle overflow:

  • NaN (not a number) e.g. 0 / 0
  • (infinity) e.g. 1 / 0
Arithmetical Operators
Operator Meaning

*

The binary * operator performs multiplication, producing the product of its operands. Multiplication is a commutative operation if the operand expressions have no side effects. While integer multiplication is associative when the operands are all of the same type, floating-point multiplication is not associative.

/

The binary / operator performs division, producing the quotient of its operands. The left-hand operand is the dividend and the right-hand operand is the divisor.

+

The binary + operator performs addition when applied to two operands of numeric type, producing the sum of the operands.

-

The binary - operator performs subtraction, producing the difference of two numeric operands.

%

The binary % operator is said to yield the remainder of its operands from an implied division; the left-hand operand is the dividend and the right-hand operand is the divisor.

In C and C++, the remainder operator accepts only integral operands, but in the Java programming language, it also accepts floating-point operands.

 

Integer Multiplication

If an integer multiplication overflows, then the result is the low-order bits of the mathematical product as represented in some sufficiently large two's-complement format. As a result, if overflow occurs, then the sign of the result may not be the same as the sign of the mathematical product of the two operand values.

Floating-Point Multiplication

The product is computed according to the rules of IEEE 754 arithmetic. The following table lists the results of all possible combinations of nonzero finite values, zeros, infinities, and NaN's. In the table, x and y are positive finite values. z is the result of x * y. If the result is too large for the destination type, z is infinity. If the result is too small for the destination type, z is zero.

  +y -y +0 -0 +∞ -∞ NaN
+x
+z
-z
+0
-0
+∞
-∞
NaN
-x
-z
+z
-0
+0
-∞
+∞
NaN
+0
+0
-0
+0
-0
NaN
NaN
NaN
-0
-0
+0
-0
+0
NaN
NaN
NaN
+∞
+∞
-∞
NaN
NaN
+∞
-∞
NaN
-∞
-∞
+∞
NaN
NaN
-∞
+∞
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN

 

Integer Division

If the value of the right operand is zero, a ArithmeticException is thrown.

class Program {
  public static void main(String[] args) {
    int x = 4; 
    System.out.println(x/0);  //run time exception ArithmeticException
  }
}

The division rounds the result towards zero, and the absolute value of the result is the largest possible integer that is less than the absolute value of the quotient of the two operands. The result is zero or positive when the two operands have the same sign and zero or negative when the two operands have opposite signs.

class Program {
  public static void main(String[] args) {
    int x = 4; 
    System.out.println(x/3);  //Output 1
    System.out.println(x/-3);  //Output -1
  }
}

There is one special case that does not satisfy this rule: if the left operand (dividend) is the smallest representable int or long value and the right operand is ?1, then an overflow occurs and the result is equal to the dividend. For example:

class Program {
  public static void main(String[] args) {
    int x = 0x80000000; //(-2147483648)
    System.out.println(x/-1); //Output -2147483648
  }
}

Floating-Point Division

The quotient is computed according to the rules of IEEE 754 arithmetic. The following table lists the results of all possible combinations of nonzero finite values, zeros, infinities, and NaN's. In the table, x and y are positive finite values. z is the result of x / y. If the result is too large for the destination type, z is infinity. If the result is too small for the destination type, z is zero.

  +y -y +0 -0 +∞ -∞ NaN
+x
+z
-z
+∞
-∞
+0
-0
NaN
-x
-z
+z
-∞
+∞
-0
+0
NaN
+0
+0
-0
NaN
NaN
+0
-0
NaN
-0
-0
+0
NaN
NaN
-0
+0
NaN
+∞
+∞
-∞
+∞
-∞
NaN
NaN
NaN
-∞
-∞
+∞
-∞
+∞
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN

 

Integer Addition

If an integer addition overflows, then the result is the low-order bits of the mathematical sum as represented in some sufficiently large two's-complement format. If overflow occurs, then the sign of the result is not the same as the sign of the mathematical sum of the two operand values.

Floating-Point Addition

The sum is computed according to the rules of IEEE 754 arithmetic. The following table lists the results of all possible combinations of nonzero finite values, zeros, infinities, and NaN's. In the table, x and y are nonzero finite values, and z is the result of x + y. If x and y have the same magnitude but opposite signs, z is positive zero. If x + y is too large to represent in the destination type, z is an infinity with the same sign as x + y.

  y +0 -0 +∞ -∞ NaN
x
z
x
x
+∞
-∞
NaN
+0
y
+0
+0
+∞
-∞
NaN
-0
y
+0
-0
+∞
-∞
NaN
+∞
+∞
+∞
+∞
+∞
NaN
NaN
-∞
-∞
-∞
-∞
NaN
-∞
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN

Integer Subtraction

The binary - operator performs subtraction when applied to two operands of numeric type producing the difference of its operands; the left-hand operand is the minuend and the right-hand operand is the subtrahend. For both integer and floating-point subtraction, it is always the case that a-b produces the same result as a+(-b).

 

Floating-Point Subtraction

The difference is computed according to the rules of IEEE 754 arithmetic. The following table lists the results of all possible combinations of nonzero finite values, zeros, infinities, and NaN's. In the table, x and y are nonzero finite values, and z is the result of x ? y. If x and y are equal, z is positive zero. If x ? y is too large to represent in the destination type, z is an infinity with the same sign as x ? y.

  y +0 -0 +∞ -∞ NaN
x
z
x
x
-∞
+∞
NaN
+0
-y
+0
+0
-∞
+∞
NaN
-0
-y
-0
+0
-∞
+∞
NaN
+∞
+∞
+∞
+∞
NaN
+∞
NaN
-∞
-∞
-∞
-∞
-∞
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN

 


Integer Remainder


The result of x % y is the value produced by x ? (x / y) * y. This identity holds even in the special case that the dividend is the negative integer of largest possible magnitude for its type and the divisor is -1 (the remainder is 0). It follows from this rule that the result of the remainder operation can be negative only if the dividend is negative, and can be positive only if the dividend is positive; moreover, the magnitude of the result is always less than the magnitude of the divisor. If the value of the divisor y for an integer remainder operator is 0, then an ArithmeticException is thrown. The remainder operator never causes an overflow.

For example:

5%3 produces 2                 (note that 5/3 produces 1)
5%(-3) produces 2               (note that 5/(-3) produces -1)
(-5)%3 produces -2              (note that (-5)/3 produces -1)
(-5)%(-3) produces -2           (note that (-5)/(-3) produces 1)

Floating-Point remainder

The following table lists the results of all possible combinations of nonzero finite values, zeros, infinities, and NaN's. In the table, x and y are positive finite values. z is the result of x % y and is computed as x ? n * y, where n is the largest possible integer that is less than or equal to x / y. This method of computing the remainder is analogous to that used for integer operands, but differs from the IEEE 754 definition (in which n is the integer closest to x / y).

  +y -y +0 -0 +∞ -∞ NaN
+x
+z
+z
NaN
NaN
x
x
NaN
-x
-z
-z
NaN
NaN
-x
-x
NaN
+0
+0
+0
NaN
NaN
+0
+0
NaN
-0
-0
-0
NaN
NaN
-0
-0
NaN
+∞
NaN
NaN
NaN
NaN
NaN
NaN
NaN
-∞
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN

Previous Next vertical dots separating previous/next from contents/index/pdf Contents

  |   |