How numbers and other data, such as characters, are represented
in memory is of great practical importance. Ideally a single memory
representation, or type, could represent all data including numbers,
characters and boolean values.
Computer memory and transfer rates, however, are not
infinite and designers must strike a compromise between the widest
possible range of values and conserving memory and maximizing speed.
Also, the data representations must use base 2 as
dictated by the underlying binary hardware. Thus floating point
numbers cannot represent all decimal numbers exactly. For example,
converting to base 2 from base 10 means 0.1 will not produce 1.0
if we sum it in a loop 10 times.
So it is clear that one universal numerical data type
will not be efficient or sufficient for all situations.
8 Primitive Data Types
For any language there is the need to provide different
storage representations for integers and floating point numbers.
In the case of integers, a variety of sizes allows
for the most efficient use of memory for a given task. For example,
type (8-bit) is often useful for I/O tasks while long
types (64-bit) are needed for representing very large values. In
between are the 16-bit short
and the 32-bit int
types. The int
type is the default for most integer operations in Java. The integer
types use two's complement signed representations.
For floating point there are two widths available.
The IEEE 754 floating point standard is used for the 32-bit float
and 64-bit double
types. See also Chapter
2: Tech : More about Floating Point.
Historically, characters were typically represented
with 7-bit ASCII codes, which allowed for 128 characters. A large
number of 8-bit encodings exist to provide both standard ASCII and
characters needed for particular languages. However, full internationalization
with a single code requires many more characters than is available
with a single byte. So a two byte character type- char
- is used to hold the Unicode representation.
(An even larger 4-byte system is under development but not implemented
in Java.) The char
type can also represent 16-bit unsigned integers.
type is needed for the many logic operations carried out in almost
Java comes with 8 primitive data types in all.
These include 4 types of integers, two floating points, a character
type and a boolean. Below is a table of the Java primitive types
with their specifications.
We will discuss classes
and objects later, but note here that primitive types in Java
are not instances of classes. The decision to use simple data types
broke the symmetry and elegance of the language to some degree but
vastly reduced overhead and execution times compared to the case
where all data types are objects.
Primitive Data Types
||-128 to 127
||-32768 to 32767
||-2147483648 to 2147483647
||-9223372036854775808 to 9223372036854775807
||IEEE 754 floating point
||+/-1.4E-45 to +/-3.4028235E+38,
+/-infinity, +/-0, NAN
||IEEE 754 floating
+/-infinity, +/-0, NaN
||\u0000 to \uFFFF
||1 bit used in 32 bit integer
More comments about primitives:
- Java is a strongly typed language. An explicit type must
be assigned to every data value.
- Converting from one type to another requires an explicit
cast when going from a higher precision type to a
lower precision type (see Casts
x = 3.1;
int i = (int)x;
type only holds true or false. In the JVM,
values are represented as integers with 0 for false and 1
for true. (The JVM specification does not specify how boolean
arrays are stored.
So it will vary according to the particular JVM implementation;
for example, as bit arrays, byte arrays, etc.)
type cannot be cast to or from other types. In C/C++ it is
common to convert back and forth between Booleans and integers.
To obtain the same effect in Java, one can use this approach:
b = (i != 0);
/* integer to boolean
In C & C++
non-zero int => true
int => false
i = (b)? 1 : 0; //
boolean to integer
is not a single byte as in C/C++ but uses two bytes.
It can also act as an unsigned integer. Casting to byte
can result in an unexpected negative value.
- The representations are the same on all machines to insure
the portability of the code. However, during calculations
involving floating point values, intermediate values can exceed
the standard exponent ranges in the table if allowed by the
particular processor. The strictfp
modifier requires that the values remain within the given
ranges throughout the calculation to insure same results on
all platforms. Further details are given in Chapter
2: Tech :More about Floating Point.
- Floating point operations that result in overflow or underflow,
or zero-divided-by-zero do not give an error message. Instead
they result in special floating point values: +/-infinity,
+/-0, NaN. These are discussed in the Chapter
2: Tech : More about Floating Point.
- For more about primitive types and numerical issues, continue
to Chapter 2:
Tech: Numerical Precision
More About the Primitive
See these other sections for additional information concernting
the primitive data types:
References & Web
Latest update: Jan.11, 2006