Java Reference
In-Depth Information
An integer literal is assumed to be of the
int
type, whose value is between
and To denote an integer literal of the
long
type, append the letter
L
or
l
to it. For example, to write integer
2147483648
in a Java pro-
gram, you have to write it as
2147483648L
or
2147483648l
, because
2147483648
exceeds
the range for the
int
value.
L
is preferred because
l
(lowercase
L
) can easily be confused
with 1 (the digit one).
2
31
(
2
31
-
-
2147483648)
-
1 (2147483647).
long
type
Note
By default, an integer literal is a decimal integer number. To denote an octal integer lit-
eral, use a leading
0
(zero), and to denote a hexadecimal integer literal, use a leading
0x
or
0X
(zero x). For example, the following code displays the decimal value
65535
for
hexadecimal number
FFFF
.
octal and hex literals
System.out.println(
0x
FFFF
)
;
Hexadecimal numbers, binary numbers, and octal numbers are introduced in Appendix F.
2.10.2 Floating-Point Literals
Floating-point literals are written with a decimal point. By default, a floating-point literal is
treated as a
double
type value. For example,
5.0
is considered a
double
value, not a
float
value. You can make a number a
float
by appending the letter
f
or
F
, and you can make a
number a
double
by appending the letter
d
or
D
. For example, you can use
100.2f
or
100.2F
for a
float
number, and
100.2d
or
100.2D
for a
double
number.
suffix f or F
suffix d or D
Note
The
double
type values are more accurate than the
float
type values. For example,
double
vs.
float
System.out.println(
"1.0 / 3.0 is "
+
1.0
/
3.0
);
displays
1.0 / 3.0 is 0.3333333333333333
.
System.out.println(
"1.0F / 3.0F is "
+
1.0F
/
3.0F
);
displays
1.0F / 3.0F is 0.33333334
.
2.10.3 Scientific Notation
Floating-point literals can be written in scientific notation in the form of For exam-
ple, the scientific notation for 123.456 is and for 0.0123456 is
A special syntax is used to write scientific notation numbers. For example,
is written as
1.23456E2
or
1.23456E+2
and as
1.23456E-2
.
E
(or
e
) represents an exponent and can be in either lowercase or uppercase.
10
b
.
a
*
10
2
1.23456
*
10
-
˛
2
.
1.23456
*
10
2
10
-
˛
2
1.23456
*
1.23456
*
Note
The
float
and
double
types are used to represent numbers with a decimal point.
Why are they called
floating-point numbers
? These numbers are stored in scientific
notation internally. When a number such as
50.534
is converted into scientific nota-
tion, such as
5.0534E+1
, its decimal point is moved (i.e., floated) to a new position.
why called floating-point?
2.15
✓
✓
Which of the following are correct literals for floating-point numbers?
Check
Point
12.3
,
12.3e+2
,
23.4e-2
,
-334.4
,
20.5
,
39F
,
40D
2.16
Which of the following are the same as
52.534
?
5.2534e+1
,
0.52534e+2
,
525.34e-1
,
5.2534e+0