CONVERSIONS AND PROMOTIONS
Narrowing Primitive Conversions
5.1.3
The example:
class Test {
public static void main(String[] args) {
float fmin = Float.NEGATIVE_INFINITY;
float fmax = Float.POSITIVE_INFINITY;
System.out.println("long: " + (long)fmin +
".." + (long)fmax);
System.out.println("int: " + (int)fmin +
".." + (int)fmax);
System.out.println("short: " + (short)fmin +
".." + (short)fmax);
System.out.println("char: " + (int)(char)fmin +
".." + (int)(char)fmax);
System.out.println("byte: " + (byte)fmin +
".." + (byte)fmax);
}
}
produces the output:
long: 9223372036854775808..9223372036854775807
int: 2147483648..2147483647
short: 0.. 1
char: 0..65535
byte: 0.. 1
The results for
char
,
int
, and
long
are unsurprising, producing the minimum
and maximum representable values of the type.
The results for
byte
and
short
lose information about the sign and magni
tude of the numeric values and also lose precision. The results can be understood
by examining the low order bits of the minimum and maximum
int.
The mini
mum
int
is, in hexadecimal,
0x80000000
, and the maximum
int
is
0x7fffffff
.
This explains the
short
results, which are the low 16 bits of these values, namely,
0x0000
and
0xffff
; it explains the
char
results, which also are the low 16 bits of
these values, namely,
\u0000
and
\uffff
; and it explains the
byte
results,
which are the low 8 bits of these values, namely,
0x00
and
0xff
.
A narrowing conversion from
double
to
float
behaves in accordance with
IEEE 754. The result is correctly rounded using IEEE 754 round to nearest mode.
A value too small to be represented as a
float
is converted to positive or negative
zero; a value too large to be represented as a
float
is converted to a (positive or
negative) infinity. A
double
NaN is always converted to a
float
NaN.
Despite the fact that overflow, underflow, or other loss of information may
occur, narrowing conversions among primitive types never result in a run time
exception ( 11).
57
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