Sunday 13 March 2016

The Java Language: Primitive Types

Numbers, characters, and Boolean values are fundamental elements in Java. Unlike some other (perhaps more pure) object-oriented languages, they are not objects. For those situations where it’s desirable to treat a primitive value as an object, Java provides “wrapper” classes. The major advantage of treating primitive values as special is that the Java compiler and runtime can more readily optimize their implementation. Primitive values and computations can still be mapped down to hardware as they always have been in lower-level languages. Later we’ll see how Java can automatically convert between primitive values and their object wrappers as needed to partially mask the difference between the two. We’ll explain what that means in more detail when we discuss boxing and unboxing of primitive values. An important portability feature of Java is that primitive types are precisely defined. For example, you never have to worry about the size of an int on a particular platform; it’s always a 32-bit, signed, two’s complement number. The following table summarizes Java’s primitive types.

Type Definition
boolean true or false
char 16-bit, Unicode character
byte 8-bit, signed, two’s complement integer
short 16-bit, signed, two’s complement integer
int 32-bit, signed, two’s complement integer
long 64-bit, signed, two’s complement integer
float 32-bit, IEEE 754, floating-point value
double 64-bit, IEEE 754

0 comments:

Post a Comment