next up previous
Next: Object Types Up: Java Data Types Previous: Java Data Types

Primitive Types

All primitive values belong to one of eight primitive types: int, float, boolean, char, byte, short, long, and double. Four of these types designate different sizes of bounded integers: In practice, only three of these primitive types are widely used: int, boolean, and double.

A program should never contain explicit references to any of the specific integer constants given above. They can always be replaced by references to the static final fields MIN_VALUE and MAX_VALUE in the corresponding ``wrapper'' class: Byte, Short, Integer or Long. Java has a built-in wrapper class for each primitive type. For example, in the wrapper class Integer corresponding to the primitive type int, MIN_VALUE = -2147483648 and MAX_VALUE = 2147483647. For more information on the primitive wrapper classes, read the on-line documentation from Sun Microsystems for the built-in class Number and its subclasses.

The boolean type has two values true and false. The char type supports the Unicode character set which includes all conventional ASCII characters plus almost any foreign character imaginable. The char type is rarely used in Java programs because a flexible String object type is built-in to the language. The remaining two types float and double are used for approximate computations involving real numbers; they denote standard IEEE 32-bit and 64-bit formats, respectively.

Numeric Constants. Java interprets unadorned integer constants as values of type int. Long integer constants are indicated by attaching the suffix L to the number. For example, the constant 9223372036854775807L can be used in Java program text, while the same constant without the L is an error because it is too big to be an int. The double type is the default type for any floating point constant. On modern machines, there is little reason to use the less precise float.

Conversions Between Types [Optional]. Java will automatically convert any numeric type to a more ``general'' numeric type demanded by context. The following list gives the primitive numeric types in increasing order of generality:

byte $\rightarrow$ short $\rightarrow$ int $\rightarrow$ long $\rightarrow$ float $\rightarrow$ double
Note that the notion of generality here is imperfect in some situations. The conversion of a long to a float, for example, will often lose precision. In fact, even the conversion of a really large long value to a double can lose precision.

Java provides explicit conversion operators called casts to convert a numeric type to a less general type. A cast is simply a type name enclosed in parentheses used a prefix operator. For example, the following expression casts the int constant 127 to the type byte

(byte) 127
When converting from one bounded integer type to another, Java silently truncates leading digits if the output type is shorter than the input type. Watch out!


Finger Exercise 1.4.1.1: In DrJava, convert the maximum long value to double. (Do not type the 20+ digits for this constant! Recall the contant fields in the wrapper class Long.) What do you get? Convert the maximum long value to type float. What do you get? Why? Try converting the maximum long value minus 1 to double and back again. Do you get the same result?


next up previous
Next: Object Types Up: Java Data Types Previous: Java Data Types
Corky Cartwright 2004-02-05