Java Reference
In-Depth Information
2.10.1 Integer Literals
An integer literal can be assigned to an integer variable as long as it can fit into the variable. A
compile error will occur if the literal is too large for the variable to hold. The statement
byte
b = 128
, for example, will cause a compile error, because
128
cannot be stored in a variable
of the
byte
type. (Note that the range for a byte value is from
-128
to
127
.)
An integer literal is assumed to be of the
int
type, whose value is between
2
31
(
2147483648) and 2
31
1 (2147483647). 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).
-
-
-
Note
By default, an integer literal is a decimal integer number. To denote a binary integer
literal, use a leading
0b
or
0B
(zero B), to denote an octal integer literal, use a leading
0
(zero), and to denote a hexadecimal integer literal, use a leading
0x
or
0X
(zero X).
For example,
binary, octal, and hex literals
System.out.println(
0B1111
);
// Displays 15
System.out.println(
07777
);
// Displays 4095
System.out.println(
0XFFFF
);
// Displays 65535
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,
System.out.println(
"1.0 / 3.0 is "
+
1.0
/
3.0
);
double
vs.
float
displays
1.0 / 3.0 is 0.3333333333333333
16 digits
System.out.println(
"1.0F / 3.0F is "
+
1.0F
/
3.0F
);
displays
1.0F / 3.0F is 0.33333334
8 digits
A float value has
7
to
8
number of significant digits and a double value has
15
to
17
number
of significant digits.
2.10.3 Scientific Notation
Floating-point literals can be written in scientific notation in the form of
a
10
b
. For example,
*
10
2
and for 0.0123456 is 1.23456
the scientific notation for 123.456 is 1.23456
*
*
10
-
2
.
A special syntax is used to write scientific notation numbers. For example, 1.23456
*
10
2
is
10
-
2
as
1.23456E-2
.
E
(or
e
) repre-
sents an exponent and can be in either lowercase or uppercase.
written as
1.23456E2
or
1.23456E+2
and 1.23456
*
Search WWH ::
Custom Search