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
the following conditions holds:
•
S
<:
T
•
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
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.