📄 5.doc.html
字号:
<a name="29348"></a> int color;
<a name="26861"></a> public void setColor(int color) { this.color = color; }
<a name="29293"></a>}
<br><br><a name="29294"></a>final class EndPoint extends Point { }
<br><a name="29295"></a>
class Test {
<a name="29296"></a>
public static void main(String[] args) {
<a name="29297"></a> Point p = new Point();
<a name="29298"></a> ColoredPoint cp = new ColoredPoint();
<a name="29299"></a> Colorable c;
<br><a name="29301"></a>
// The following may cause errors at run time because
<a name="29302"></a> // we cannot be sure they will succeed; this possibility
<a name="29303"></a> // is suggested by the casts:
<a name="29304"></a> cp = (ColoredPoint)p; // p might not reference an
<a name="29380"></a> // object which is a ColoredPoint
<a name="29381"></a> // or a subclass of ColoredPoint
<a name="29307"></a> c = (Colorable)p; // p might not be Colorable
<br><a name="29309"></a>
// The following are incorrect at compile time because
<a name="29310"></a> // they can never succeed as explained in the text:
<a name="29311"></a> Long l = (Long)p; // compile-time error #1
<a name="50095"></a> EndPoint e = new EndPoint();
<a name="29316"></a> c = (Colorable)e; // compile-time error #2
<br><a name="29313"></a> }
<br><a name="29314"></a>}
</pre><a name="29315"></a>
Here the first compile-time error occurs because the class types <code>Long</code> and <code>Point</code>
are unrelated (that is, they are not the same, and neither is a subclass of the other),
so a cast between them will always fail.
<p><a name="176389"></a>
The second compile-time error occurs because a variable of type <code>EndPoint</code> can never reference a value that implements the interface <code>Colorable</code>. This is because <code>EndPoint</code> is a <code>final</code> type, and a variable of a <code>final</code> type always holds a value of the same run-time type as its compile-time type. Therefore, the run-time type of variable <code>e</code> must be exactly the type <code>EndPoint</code>, and type <code>EndPoint</code> does not implement <code>Colorable</code>.<p>
<a name="176390"></a>
Here is an example involving arrays <a href="10.doc.html#27803">(§10)</a>: <p>
<pre><a name="176391"></a>
class Point {
<a name="53226"></a>
int x, y;
<br><a name="53241"></a> Point(int x, int y) { this.x = x; this.y = y; }
<br><a name="53227"></a> public String toString() { return "("+x+","+y+")"; }<br>
}
<br><a name="30080"></a>public interface Colorable { void setColor(int color); }
<br><a name="30081"></a>
public class ColoredPoint extends Point implements Colorable
{
<a name="53235"></a>
int color;
<a name="26864"></a>
ColoredPoint(int x, int y, int color) {
<a name="53230"></a> super(x, y); setColor(color);
<a name="53231"></a> }
<br><br><a name="30084"></a> public void setColor(int color) { this.color = color; }
<br><a name="53225"></a> public String toString() {
<a name="53228"></a> return super.toString() + "@" + color;
<a name="53229"></a> }
<br><a name="30085"></a>}
<br><a name="30086"></a>
class Test {
<a name="30128"></a>
public static void main(String[] args) {
<a name="30129"></a> Point[] pa = new ColoredPoint[4];
<a name="30135"></a> pa[0] = new ColoredPoint(2, 2, 12);
<a name="50097"></a> pa[1] = new ColoredPoint(4, 5, 24);
<a name="30130"></a> ColoredPoint[] cpa = (ColoredPoint[])pa;
<a name="30143"></a> System.out.print("cpa: {");
<a name="30140"></a> for (int i = 0; i < cpa.length; i++)
<a name="30108"></a> System.out.print((i == 0 ? " " : ", ") + cpa[i]);
<a name="53256"></a> System.out.println(" }");
<a name="30099"></a> }
<br><a name="30100"></a>}
</pre><a name="53223"></a>
This example compiles without errors and produces the output:
<p><pre><a name="53243"></a>cpa: { (2,2)@12, (4,5)@24, null, null }
</pre><a name="176370"></a>
The following example uses casts to compile, but it throws exceptions at run time, because the types are incompatible:<p>
<pre><a name="175285"></a>
public class Point { int x, y; }
<br><a name="29436"></a>public interface Colorable { void setColor(int color); }
<br><a name="29438"></a>
public class ColoredPoint extends Point implements Colorable
{
<br><a name="29439"></a> int color;
<br><br><a name="29441"></a> public void setColor(int color) { this.color = color; }
<br><a name="29442"></a>}
<br><a name="176415"></a>
class Test {
<a name="13438"></a>
public static void main(String[] args) {
<br><a name="29551"></a> Point[] pa = new Point[100];
<br><a name="29560"></a>
// The following line will throw a ClassCastException:
<a name="29460"></a> ColoredPoint[] cpa = (ColoredPoint[])pa;
<br><br><a name="30037"></a> System.out.println(cpa[0]);
<br><br><a name="13452"></a> int[] shortvec = new int[2];
<br><br><a name="13453"></a> Object o = shortvec;
<br><a name="29468"></a>
// The following line will throw a ClassCastException:
<a name="13454"></a> Colorable c = (Colorable)o;
<br><br><a name="30034"></a> c.setColor(0);
<br><a name="13456"></a> }
<br><a name="13457"></a>}
</pre><a name="26917"></a>
<h2>5.6 Numeric Promotions</h2>
<a name="26918"></a>
<i>Numeric promotion</i> is applied to the operands of an arithmetic operator. Numeric
promotion contexts allow the use of an identity conversion <a href="5.doc.html#25209">(§5.1.1)</a> or a widening
primitive conversion <a href="5.doc.html#25222">(§5.1.2)</a>.
<p><a name="53304"></a>
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 <a href="5.doc.html#170952">(§5.6.1)</a> and binary numeric promotion<i> </i><a href="5.doc.html#170983">(§5.6.2)</a>. The analogous conversions in C are called "the usual unary conversions" and "the usual binary conversions."<p>
<a name="170949"></a>
Numeric promotion is not a general feature of Java, but rather a property of the specific definitions of the built-in operations.<p>
<a name="170952"></a>
<h3>5.6.1 Unary Numeric Promotion</h3>
<a name="170954"></a>
Some operators apply <i>unary numeric promotion</i> to a single operand, which must
produce a value of a numeric type:
<p><ul><a name="20278"></a>
<li>If the operand is of compile-time type <code>byte</code>, <code>short</code>, or <code>char</code>, unary numeric promotion promotes it to a value of type <code>int</code> by a widening conversion <a href="5.doc.html#25222">(§5.1.2)</a>.
<a name="20279"></a>
<li>Otherwise, a unary numeric operand remains as is and is not converted.
</ul><a name="23183"></a>
Unary numeric promotion is performed on expressions in the following situations:
<p><ul><a name="175745"></a>
<li>The dimension expression in array creations <a href="15.doc.html#46168">(§15.9)</a>
<a name="23187"></a>
<li>The index expression in array access expressions <a href="15.doc.html#239587">(§15.12)</a>
<a name="170969"></a>
<li>Operands of the unary operators plus <code>+</code> <a href="15.doc.html#24924">(§15.14.3)</a> and minus <code>-</code> <a href="15.doc.html#236345">(§15.14.4)</a>
<a name="170977"></a>
<li>The operand of the bitwise complement operator <code>~</code> <a href="15.doc.html#5017">(§15.14.5)</a>
<a name="170981"></a>
<li>Each operand, separately, of the shift operators <code>>></code>, <code>>>></code>, and <code><<</code> <a href="15.doc.html#5121">(§15.18)</a>, so that a <code>long</code> shift distance (right operand) does not promote the value being shifted (left operand) to <code>long</code>
</ul><a name="53322"></a>
Here is a test program that includes examples of unary numeric promotion:
<p><pre><a name="7300"></a>
class Test {
<a name="13473"></a> public static void main(String[] args) {
<a name="13474"></a> byte b = 2;
<a name="47059"></a> int a[] = new int[b]; // dimension expression promotion
<a name="47060"></a> char c = '\u0001';
<a name="47061"></a> a[c] = 1; // index expression promotion
<a name="13490"></a> a[0] = -c; // unary - promotion
<a name="17404"></a> System.out.println("a: " + a[0] + "," + a[1]);
<a name="13522"></a>
b = -1;
<a name="39072"></a> int i = ~b; // bitwise complement promotion
<a name="17405"></a> System.out.println("~0x" + Integer.toHexString(b)
<a name="174297"></a> + "==0x" + Integer.toHexString(i));
<a name="13523"></a>
i = b << 4L; // shift promotion (left operand)
<a name="17411"></a> System.out.println("0x" + Integer.toHexString(b)
<a name="174298"></a> + "<<4L==0x" + Integer.toHexString(i));
<a name="13525"></a> }
<a name="13526"></a>}
</pre><a name="30172"></a>
This test program produces the output:
<p><pre><a name="39118"></a>
a: -1,1
<a name="39119"></a>~0xffffffff==0x0
<a name="30173"></a>0xffffffff<<4L==0xfffffff0
</pre><a name="170983"></a>
<h3>5.6.2 Binary Numeric Promotion</h3>
<a name="170985"></a>
When an operator applies <i>binary numeric promotion</i> to a pair of operands, each of
which must denote a value of a numeric type, the following rules apply, in order,
using widening conversion <a href="5.doc.html#25222">(§5.1.2)</a> to convert operands as necessary:
<p><ul><a name="170986"></a>
<li>If either operand is of type <code>double</code>, the other is converted to <code>double</code>.
<a name="170987"></a>
<li>Otherwise, if either operand is of type <code>float</code>, the other is converted to <code>float</code>.
<a name="170988"></a>
<li>Otherwise, if either operand is of type <code>long</code>, the other is converted to <code>long</code>.
<a name="170989"></a>
<li>Otherwise, both operands are converted to type <code>int</code>.
</ul><a name="170990"></a>
Binary numeric promotion is performed on the operands of certain operators:
<p><ul><a name="170994"></a>
<li>The multiplicative operators <code>*</code>, <code>/</code> and <code>%</code> <a href="15.doc.html#239829">(§15.16)</a>
<a name="170998"></a>
<li>The addition and subtraction operators for numeric types <code>+</code> and <code>- </code><a href="15.doc.html#13510">(§15.17.2)</a>
<a name="171002"></a>
<li>The numerical comparison operators <code><</code>, <code><=</code>, <code>></code>, and <code>>=</code> <a href="15.doc.html#153654">(§15.19.1)</a>
<a name="171006"></a>
<li>The numerical equality operators <code>==</code> and <code>!=</code> <a href="15.doc.html#5198">(§15.20.1)</a>
<a name="171010"></a>
<li>The integer bitwise operators <code>&</code>, <code>^</code>, and <code>|</code> <a href="15.doc.html#5233">(§15.21.1)</a>
<a name="171014"></a>
<li>In certain cases, the conditional operator <code>? 
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -