Chapter 3. Primitive Types, Wrappers, and Boxing

  • Literal Values

  • Boolean

  • Char

  • INT

  • Long

  • Byte

  • Short

  • Limited Accuracy of Floating Point Types

  • Double

  • Float

  • Object Wrappers for Primitives

  • Autoboxing and Unboxing

  • Performance Implications of Autoboxing

  • Java.lang.Object

  • Java.lang.String

  • Language Support for String Concatenation

  • String Comparison

  • Some Light Relief—Hatless Atlas

There are eight built-in, non-object types in Java, known as primitive types. Every piece of data in a class is ultimately represented in terms of these primitive types. The eight primitive types are:

  • boolean (for true/false values)

  • char (for character data, ultimately to be input or printed)

  • int, long, byte, short (for arithmetic on whole numbers)

  • double, float (for arithmetic on the real numbers)

Primitive types are simpler than class types, and are directly supported in hardware on most computers. That is, most of the operations on primitive types are a single machine instruction, like ADD, whereas the operations on class types are statements in a method.

Older high-level languages don't specify the sizes of primitive data types. On an 8086 an int was naturally 16 bits, but on the first SPARC processors it was 32 bits. Compiler writers for C were allowed to select the best sizes on each architecture for performance. That turned out to be a Very Bad Idea. Types that vary with the platform greatly impede program portability, and programmer time is a lot more expensive than processor time.

Java does away with all the uncertainty by rigorously specifying the sizes of the primitive types and making clear that these sizes are identical on all platforms. We'll examine the properties of each primitive type in turn.

Literal Values

As we mention each of the eight primitive types, we'll show a typical declaration; say what range of values it can hold; and also describe the literal values for the type.

A “literal” is a value provided at compile time. Just write down the value that you mean, and that's the literal. Here's a snippet of code showing several literals:

int  i = 2;       // 2 is a literal
double d = 3.14;  // 3.14 is a literal
if ( c == 'j' )     // 'j' is a literal

Every literal has a type, just like every variable has a type. The literal written as 2 has type int. The literal written as 3.14 belongs to the type double. For booleans, the literals are the words false and true.

It is not valid to directly assign a literal of one type to a variable of another. In other words, this code is invalid:

int  i = 3.14;  // type mismatch! BAD CODE

The literal 3.14 is a double floating-point number literal, and cannot be assigned directly to an int variable. You can do it by forcing an explicit type conversion, known as a cast. The details are coming up shortly.

A language that allows assignment and conversion only between closely related types is called a strongly typed language. Java is a strongly typed language. Strong typing reduces the power of a language, but reduces some errors even more.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.7.131