Java Reference
In-Depth Information
T
ABLE
2.2
Numeric Data Types
Name
Range
Storage Size
2
7
2
7
8-bit signed
byte
-
to
-
1
(
-
128 to 127)
byte
type
2
15
2
15
short
-
to
-
1
(
-
32768 to 32767)
16-bit signed
short
type
2
31
2
31
int
-
to
-
1
(
-
2147483648
to 2147483647)
32-bit signed
int
type
2
63
2
63
-
-
64-bit signed
long
to
1
long
type
(i.e.,
-
9223372036854775808
to 9223372036854775807)
float
Negative range:
-
3.4028235E
+
38
to
-
1.4E
-
45
32-bit IEEE 754
float
type
1.4E
-
45
to 3.4028235E
+
38
Positive range:
-
+
-
-
double
Negative range:
1.7976931348623157E
308
to
4.9E
324
64-bit IEEE 754
double
type
Positive range:
4.9E
-
324
to 1.7976931348623157E
+
308
Note
IEEE 754
is a standard approved by the Institute of Electrical and Electronics Engineers
for representing floating-point numbers on computers. The standard has been widely
adopted. Java uses the 32-bit
IEEE 754
for the
float
type and the 64-bit
IEEE 754
for
the
double
type. The
IEEE 754
standard also defines special floating-point values,
which are listed in Appendix E.
Java uses four types for integers:
byte
,
short
,
int
, and
long
. Choose the type that is
most appropriate for your variable. For example, if you know an integer stored in a variable is
within a range of a byte, declare the variable as a
byte
. For simplicity and consistency, we
will use
int
for integers most of the time in this topic.
Java uses two types for floating-point numbers:
float
and
double
. The
double
type is
twice as big as
float
, so the
double
is known as
double precision
and
float
as
single
precision.
Normally you should use the
double
type, because it is more accurate than the
float
type.
integer types
floating-point types
Caution
When a variable is assigned a value that is too large (
in size
) to be stored, it causes
overflow.
For example, executing the following statement causes overflow, because the
largest value that can be stored in a variable of the
int
type is
2147483647
.
2147483648
will be too large for an
int
value.
what is overflow?
int
value =
2147483647
+
1
;
// value will actually be -2147483648
Likewise, executing the following statement causes overflow, because the smallest value
that can be stored in a variable of the
int
type is
-2147483648
.
-2147483649
will
be too large in size to be stored in an
int
variable.
int
value =
-2147483648
-
1
;
// value will actually be 2147483647
Java does not report warnings or errors on overflow, so be careful when working with
numbers close to the maximum or minimum range of a given type.