Java Reference
In-Depth Information
//
An example of inexact results and rounding:
System.out.print("inexact results with float: ");
for
(
int
i= 0; i < 1000; i= i + 1) {
double
z= 1.0D / i;
if
(z * i != 1.0D) {
System.out.print( " " + i);
}
}
A complete discussion of
double
numbers is beyond the scope of this topic.
For more information about
double
, look at the specification of Java and turn to
more advanced topics on programming and numerical analysis.
6.4.2
Type float
Type
float
is similar to type
double
, except that a
float
value occupies four
bytes instead of eight. Its negative and positive values are:
-3.4028235E38
to
-1.4E-45
1.4E-45
to
3.4028235E38
In most situations, type
double
is used and not
float
because the use of
double
gives much more accuracy. However, when space is at a premium and
accuracy is not important, use
float
.
float literals
To change a double literal (that does not include
d
or
D
) into a float literal,
follow it (with no separating whitespace) by a lower case or upper case
F
. For
example,
3.0
and
5D
are
double
literals, but
3.0F
and
5F
are
float
literals.
Operations of type float
The operations of type
float
are similar to those of type
double
. They are:
negation, unary addition, addition, subtraction, multiplication, division, and
remainder. Given
float
operands, they produce a
float
result. See Sec. 6.1 for
a description of the operations and Sec. 6.6 for a discussion of casting.
6.5
Type char
Type
char
has as its values the set of characters that you can process in a Java
program.
The constants of this type, called
literals
of type
char
, are single characters
enclosed in single-quote marks, e.g.
'A'
and
';'
. A sequence of these characters
enclosed within double quotes
"
form a literal of class
String
. For example,
String
literal
Activity
13-7
"A;a"
Search WWH ::
Custom Search