Java Reference
In-Depth Information
double d = Math.sin(f);
// Two string conversions of f and d:
System.out.println("Math.sin(" + f + ")==" + d);
}
}
This program produces the output:
(int)12.5f==12
after float widening: 12.0
12.0*12==144.0
Math.sin(144.0)==-0.49102159389846934
5.1. Kinds of Conversion
Specific type conversions in the Java programming language are divided into 13 categories.
5.1.1. Identity Conversion
A conversion from a type to that same type is permitted for any type.
This may seem trivial, but it has two practical consequences. First, it is always permit-
ted for an expression to have the desired type to begin with, thus allowing the simply
stated rule that every expression is subject to conversion, if only a trivial identity con-
version. Second, it implies that it is permitted for a program to include redundant cast
operators for the sake of clarity.
5.1.2. Widening Primitive Conversion
19 specific conversions on primitive types are called the
widening primitive conversions
:
•
byte
to
short
,
int
,
long
,
float
, or
double
•
short
to
int
,
long
,
float
, or
double
•
char
to
int
,
long
,
float
, or
double
•
int
to
long
,
float
, or
double
•
long
to
float
or
double
•
float
to
double
A widening primitive conversion does not lose information about the overall magnitude of
a numeric value.
A widening primitive conversion from an integral type to another integral type, or from
float
to
double
in a
strictfp
expression (§
15.4
), does not lose any information at all; the numer-
ic value is preserved exactly.