2.1 The Integer Types and Operations Table 2-1 shows the bit size , the minimum value, and the maximum value of each of Java's integer types. Except for type char, which does not have negative values, the absolute value of the minimum value of each type is one greater than the maximum value of that type. (We'll see in Section 2.2 why this is so.) Except for char, exactly one half of each type's values are negative, and the other half consists of 0 and the positive values. Table 2-1. Java's integer types.
Table 2-2. The type of the result depends on the types of the operands when performing the integer addition, subtraction, multiplication, division, and remainder arithmetic operations.
Java supports various arithmetic operations on int and long values. At run time, it automatically converts byte, short, and char values to int before it performs an operation. The additive operations are addition and subtraction, and the multiplicative operations are multiplication, division, and remainder. The postfix operations are postincrement and postdecrement, and the unary operations include negation, pre-increment , and predecrement. The result type of a postfix or a unary operation is the same as the type of its single operand. Table 2-2 shows how the result type of an additive or a multiplicative operation depends on the types of its two operands. The result is type int only if both operands are type int. Even if we multiply two int values, the product is an int, not a long. Integer arithmetic never throws an exception if an overflow occurs. (We'll see in Section 2.3 what really happens during an overflow.) The only exception that it does throw is an ArithmeticException if we attempt to divide by zero during a division or a remainder operation. |
Top |