Java Reference
In-Depth Information
Table 3-2
Java Primitive Data Types
TYPE
DESCRIPTION
EXAMPLES
boolean
stores data in only one of two states, as a
true
logical value of true or false
false
byte
stores whole number values in 8-bit signed
75
locations from -128 to +127
-14
char
stores any one of the 65,436 single
'a'
characters of the Unicode set, which
'M'
includes characters and symbols from
many languages
double
stores numbers with up to 14 or 15 decimal
87.266975314
places as double-precision, floating-point
100D
values
float
stores numbers with up to 6 or 7 decimals
349.135
as single-precision, floating-point values
954F
int
stores whole number values in 32-bit signed
29387
locations from -2 31 to +2 31 -1
-86421
long
stores whole number values in 64-bit signed
13579286740
locations from approximately
7362L
-9 * 10 18 to +9 * 10 18 -1
short
stores whole number values in 16-bit signed
619
-530
locations from -32,768 to +32,767
As you learned in Chapter 2, when an actual number or character displays as
data in code, it is called a literal. Generally speaking, a literal numeric value with
no decimal point is treated by the compiler as an int data type. Programmers can
specify that the data be treated as a long integer by putting an uppercase L or
lowercase l after the number, which overrides the default int storage. An upper-
case L is preferred, as it cannot be confused with the digit 1. In the same way, an
uppercase D or lowercase d after the literal forces that number to be considered a
double. The compiler considers a literal containing digits and a decimal point as
a double. Programmers may override that storage to specify a float by putting an
F or f after the number.
The two boolean literals are true and false. Programmers use the boolean
data type to store a comparative result or one that has only two states — such as
true or false, yes or no, 1 or 0 — as a logical value of true or false. A literal char
value is any single character between single quote marks.
Primitive Data Types Are Platform-Independent
In other programming languages, the format and size of primitive
data types may depend on the platform on which a program is
running. In contrast, the Java programming language specifies the
size and format of its primitive data types. Therefore, primitive
data types are platform-independent, which means programmers
do not have to worry about system dependencies.
 
Search WWH ::




Custom Search