CONVERSIONS AND PROMOTIONS
Narrowing Primitive Conversions
5.1.3
resulting floating point value will be a correctly rounded version of the integer
value, using IEEE 754 round to nearest mode ( 4.2.4).
A widening conversion of a signed integer value to an integral type
T
simply
sign extends the two's complement representation of the integer value to fill the
wider format. A widening conversion of a character to an integral type
T
zero
extends the representation of the character value to fill the wider format.
Despite the fact that loss of precision may occur, widening conversions
among primitive types never result in a run time exception ( 11).
Here is an example of a widening conversion that loses precision:
class Test {
public static void main(String[] args) {
int big = 1234567890;
float approx = big;
System.out.println(big (int)approx);
}
}
which prints:
46
thus indicating that information was lost during the conversion from type
int
to
type
float
because values of type
float
are not precise to nine significant digits.
5.1.3 Narrowing Primitive Conversions
The following 23 specific conversions on primitive types are called the
narrowing
primitive conversions
:
byte
to
char
short
to
byte
or
char
char
to
byte
or
short
int
to
byte
,
short
, or
char
long
to
byte
,
short
,
char
, or
int
float
to
byte
,
short
,
char
,
int
, or
long
double
to
byte
,
short
,
char
,
int
,
long
, or
float
Narrowing conversions may lose information about the overall magnitude of a
numeric value and may also lose precision.
A narrowing conversion of a signed integer to an integral type
T
simply dis
cards all but the
n
lowest order bits, where
n
is the number of bits used to repre
55
footer
Our partners:
PHP: Hypertext Preprocessor Best Web Hosting
Java Web Hosting
Inexpensive Web Hosting
Jsp Web Hosting
Cheapest Web Hosting
Jsp Hosting
Cheap Hosting
Visionwebhosting.net Business web hosting division of Web
Design Plus. All rights reserved