2.1 The Integer Types and Operations

   

 
Java Number Cruncher: The Java Programmer's Guide to Numerical Computing
By Ronald  Mak

Table of Contents
Chapter  2.   How Wholesome Are the Integers?

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.

Integer Type

Size (Bits)

Minimum Value

Maximum Value

byte

8

-128

127

short

16

-32,768

32,767

char

16

65,535

int

32

-2,147,483,648

2,147,483,647

long

64

-9,223,372,036,854,775,808

9,223,372,036,854,775,807

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.

Operand 1

Operand 2

Result

int

int

int

int

long

long

long

int

long

long

long

long

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
 


Java Number Cruncher. The Java Programmer's Guide to Numerical Computing
Java Number Cruncher: The Java Programmers Guide to Numerical Computing
ISBN: 0130460419
EAN: 2147483647
Year: 2001
Pages: 141
Authors: Ronald Mak

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net