Java Reference
In-Depth Information
T
ABLE
2.1
Numeric Data Types
Name
Range
Storage Size
8-bit signed
byte
-
2
7
to 2
7
byte
type
-
1(
-
128 to 127)
short
16-bit signed
-
2
15
to 2
15
short
type
-
1(
-
32768 to 32767)
int
32-bit signed
-
2
31
to 2
31
int
type
-
1(
-
2147483648 to 2147483647)
long
64-bit signed
2
63
to 2
63
long
type
-
-
1
(i.e.,
-
9223372036854775808 to 9223372036854775807)
Negative range:
-
3.4028235E
+
38 to
-
1.4E
-
45
32-bit IEEE 754
float
float
type
Positive range: 1.4E
-
45 to 3.4028235E
+
38
double
Negative range:
-
1.7976931348623157E
+
308 to
64-bit IEEE 754
double
type
-
-
4.9E
324
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
2.9.2 Reading Numbers from the Keyboard
You know how to use the
nextDouble()
method in the
Scanner
class to read a double
value from the keyboard. You can also use the methods listed in Table 2.2 to read a number
of the
byte
,
short
,
int
,
long
, and
float
type.
T
ABLE
2.2
Methods for
Scanner
Objects
Method
Description
nextByte()
reads an integer of the
byte
type.
nextShort()
reads an integer of the
short
type.
nextInt()
reads an integer of the
int
type.
nextLong()
reads an integer of the
long
type.
nextFloat()
reads a number of the
float
type.
nextDouble()
reads a number of the
double
type.
Search WWH ::
Custom Search