5.1.4
Widening Reference Conversions
CONVERSIONS AND PROMOTIONS
Here is a small test program that demonstrates a number of narrowing conver
sions that lose information:
class Test {
public static void main(String[] args) {
//
A narrowing of
int
to
short
loses high bits:
System.out.println("(short)0x12345678==0x" +
Integer.toHexString((short)0x12345678));
//
A
int
value not fitting in
byte
changes sign and magnitude:
System.out.println("(byte)255==" + (byte)255);
//
A
float
value too big to fit gives largest
int
value:
System.out.println("(int)1e20f==" + (int)1e20f);
//
A NaN converted to
int
yields zero:
System.out.println("(int)NaN==" + (int)Float.NaN);
//
A
double
value too large for
float
yields infinity:
System.out.println("(float) 1e100==" + (float) 1e100);
//
A
double
value too small for
float
underflows to zero:
System.out.println("(float)1e 50==" + (float)1e 50);
}
}
This test program produces the following output:
(short)0x12345678==0x5678
(byte)255== 1
(int)1e20f==2147483647
(int)NaN==0
(float) 1e100== Infinity
(float)1e 50==0.0
5.1.4 Widening Reference Conversions
The following conversions are called the
widening reference conversions
:
From any class type
S
to any class type
T
, provided that
S
is a subclass of
T
.
(An important special case is that there is a widening conversion to the class
type
Object
from any other class type.)
From any class type
S
to any interface type
K
, provided that
S
implements
K
.
From the null type to any class type, interface type, or array type.
From any interface type
J
to any interface type
K
, provided that
J
is a sub
interface of
K
.
58
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