Java Reference
In-Depth Information
Additional Java Syntax
This appendix briefly covers several features of Java that are not otherwise shown in
the textbook. It is not intended to be a complete reference; see Sun's Java Language
Specification and Java Tutorial online for more detailed coverage of these language
features.
Primitive Types:
byte
,
short
,
long
,
float
In this textbook we have focused our attention on four primitive data types:
int
,
double
,
char
, and
boolean
. Java has four additional primitive types that are used in
certain special situations. Three of these additional types (
byte
,
short
, and
long
)
are variants of
int
that use a different amount of memory, providing a tradeoff
between memory consumption and the range of numbers that can be represented. The
fourth,
float
, is a variant of
double
that uses half the memory. It can be useful in
certain applications where reduced memory usage and faster computation is more
important than extremely high numeric accuracy, such as in computer games.
Type
Description
Range
Usage
8-bit (1-byte) integer
-
128 to 127
Reading data from an input source one
byte at a time
byte
short
16-bit (2-byte) integer
-
32,768 to 32,767
Saving memory when creating many
integers
64-bit (8-byte) integer
Representing very large integers that
may not fit into the range of
int
-
2
63
to (2
63
-
1)
long
float
32-bit (4-byte) real
number
roughly 3.4E
-
+
38
Saving memory when creating many
real numbers
to 3.4E
+
38
Many of the operators and much of the syntax that you have used with
int
and
double
work with these types. Two of these types use a suffix letter at the end of
their literal values:
F
for float, and
L
for
long
. The following code declares a variable
of each type:
byte var1 = 63;
short var2 = -16000;
long var3 = 1234567890123456L;
float var4 = 1.2345F;
1128
Search WWH ::
Custom Search