Java Reference
In-Depth Information
You learn how to declare and use array variables on Day 4. Today's lesson focuses on the
other variable types.
Data Types
There are eight basic data types for the storage of integers, floating-point numbers, char-
acters, and Boolean values. These often are called primitive types because they are built-
in parts of the Java language rather than objects, which makes them more efficient to use.
These data types have the same size and characteristics no matter what operating system
and platform you're on, unlike some data types in other programming languages.
There are four data types you can use to store integers. Which one you use depends on
the size of the integer, as indicated in Table 2.1.
TABLE 2.1
Integer Types
Type
Size
Values That Can Be Stored
8 bits
128 to 127
byte
16 bits
32,768 to 32,767
short
32 bits
2,147,483,648 to 2,147,483,647
int
64 bits
9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
long
All these types are signed, which means that they can hold either positive or negative
numbers. The type used for a variable depends on the range of values it might need to
hold. None of these integer variables can reliably store a value that is too large or too
small for its designated variable type, so take care when designating the type.
Another type of number that can be stored is a floating-point number, which has the type
float or double . Floating-point numbers are numbers with a decimal point. The float
type should be sufficient for most uses because it can handle any number from 1.4E-45
to 3.4E+38 . If not, the double type can be used for more precise numbers ranging from
4.9E-324 to 1.7E+308 .
The char type is used for individual characters, such as letters, numbers, punctuation,
and other symbols.
The last of the eight primitive data types is boolean . As you have learned, this data type
holds either true or false in Java.
All these variable types are listed in lowercase, and you must use them as such in pro-
grams. There are classes with the same names as some of these data types but with dif-
ferent capitalization—for example, Boolean and Char . These have different functionality
in a Java program, so you can't use them interchangeably. Tomorrow you will see how to
use these special classes.
 
Search WWH ::




Custom Search