Java Reference
In-Depth Information
Point[] pa = new ColoredPoint[4];
pa[0] = new ColoredPoint(2, 2, 12);
pa[1] = new ColoredPoint(4, 5, 24);
ColoredPoint[] cpa = (ColoredPoint[])pa;
System.out.print("cpa: {");
for (int i = 0; i < cpa.length; i++)
System.out.print((i == 0 ? " " : ", ") + cpa[i]);
System.out.println(" }");
}
}
This program compiles without errors and produces the output:
cpa: { (2,2)@12, (4,5)@24, null, null }
5.5.2. Checked Casts and Unchecked Casts
A cast from a type S to a type T is statically known to be correct if and only if S <: T 4.10 ) .
A cast from a type S to a parameterized type (§ 4.5 ) T is unchecked unless at least one of
the following conditions holds:
S <: T
• All of the type arguments (§ 4.5.1 ) of T are unbounded wildcards
T <: S and S has no subtype X other than T where the type arguments of X are not
contained in the type arguments of T .
A cast from a type S to a type variable T is unchecked unless S <: T .
An unchecked cast from S to T is completely unchecked if the cast from | S | to | T | is statically
known to be correct. Otherwise, it is partially unchecked .
An unchecked cast causes a compile-time unchecked warning, unless suppressed by the
SuppressWarnings annotation (§ 9.6.3.5 ) .
A cast is checked if it is not statically known to be correct and it is not unchecked. If a cast
to a reference type is not a compile-time error, there are several cases:
• The cast is statically known to be correct.
No run-time action is performed for such a cast.
• The cast is a completely unchecked cast.
No run-time action is performed for such a cast.
• The cast is a partially unchecked cast.
Search WWH ::




Custom Search