The Java programming language provides a number of operators that act on integral values:
-
The comparison operators, which result in a value of type
boolean: -
The numerical operators, which result in a value of type
intorlong:- The unary plus and minus operators
+and-(§15.15.3, §15.15.4) - The multiplicative operators
*,/, and%(§15.17) - The additive operators
+and-(§15.18) - The increment operator
++, both prefix (§15.15.1) and postfix (§15.14.2) - The decrement operator
--, both prefix (§15.15.2) and postfix (§15.14.3) - The signed and unsigned shift operators
<<,>>, and>>>(§15.19) - The bitwise complement operator
~(§15.15.5) - The integer bitwise operators
&,^, and|(§15.22.1)
- The unary plus and minus operators
-
The conditional operator
? :(§15.25) -
The cast operator (§15.16), which can convert from an integral value to a value of any specified numeric type
-
The string concatenation operator
+(§15.18.1), which, when given aStringoperand and an integral operand, will convert the integral operand to aString(the decimal form of abyte,short,int, orlongoperand, or the character of acharoperand), and then produce a newly createdStringthat is the concatenation of the two strings
Other useful constructors, methods, and constants are predefined in the classes Byte, Short, Integer, Long, and Character.
If an integer operator other than a shift operator has at least one operand of type long, then the operation is carried out using 64-bit precision, and the result of the numerical operator is of type long. If the other operand is not long, it is first widened (§5.1.5) to type long by numeric promotion (§5.6).
Otherwise, the operation is carried out using 32-bit precision, and the result of the numerical operator is of type int. If either operand is not an int, it is first widened to type int by numeric promotion.
Any value of any integral type may be cast to or from any numeric type. There are no casts between integral types and the type boolean.
See §4.2.5 for an idiom to convert integer expressions to boolean.
The integer operators do not indicate overflow or underflow in any way.
An integer operator can throw an exception (§11 (Exceptions)) for the following reasons:
- Any integer operator can throw a
NullPointerExceptionif unboxing conversion (§5.1.8) of a null reference is required. - The integer divide operator
/(§15.17.2) and the integer remainder operator%(§15.17.3) can throw anArithmeticExceptionif the right-hand operand is zero. - The increment and decrement operators
++(§15.14.2, §15.15.1) and--(§15.14.3, §15.15.2) can throw anOutOfMemoryErrorif boxing conversion (§5.1.7) is required and there is not sufficient memory available to perform the conversion.
Example 4.2.2-1. Integer Operations
class Test {
public static void main(String[] args) {
int i = 1000000;
System.out.println(i * i);
long l = i;
System.out.println(l * l);
System.out.println(20296 / (l - i));
}
}
This program produces the output:
-727379968
1000000000000
and then encounters an ArithmeticException in the division by l - i, because l - i is zero. The first multiplication is performed in 32-bit precision, whereas the second multiplication is a long multiplication. The value -727379968 is the decimal value of the low 32 bits of the mathematical result, 1000000000000, which is a value too large for type int.