5.6
Numeric Promotions
CONVERSIONS AND PROMOTIONS
The following example uses casts to compile, but it throws exceptions at run
time, because the types are incompatible:
public class Point { int x, y; }
public interface Colorable { void setColor(int color); }
public class ColoredPoint extends Point implements Colorable
{
int color;
public void setColor(int color) { this.color = color; }
}
class Test {
public static void main(String[] args) {
Point[] pa = new Point[100];
//
The following line will throw a
ClassCastException
:
ColoredPoint[] cpa = (ColoredPoint[])pa;
System.out.println(cpa[0]);
int[] shortvec = new int[2];
Object o = shortvec;
//
The following line will throw a
ClassCastException
:
Colorable c = (Colorable)o;
c.setColor(0);
}
}
5.6 Numeric Promotions
Numeric promotion
is applied to the operands of an arithmetic operator. Numeric
promotion contexts allow the use of an identity conversion ( 5.1.1) or a widening
primitive conversion ( 5.1.2).
Numeric promotions are used to convert the operands of a numeric operator to
a common type so that an operation can be performed. The two kinds of numeric
promotion are unary numeric promotion ( 5.6.1) and binary numeric promotion
( 5.6.2). The analogous conversions in C are called the usual unary conversions
and the usual binary conversions.
Numeric promotion is not a general feature of Java, but rather a property of
the specific definitions of the built in operations.
72
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