Why doesn't calling Object.clone () require an explicit cast? Isn't that an exception to the rule "downcast always need and explicit cast"? I compiled and successfully executed the following code using both the javac command line and Eclipse Helios with JDK1.6.0_29.
public class Main {
public static void main(String[] args) {
byte[] original = { 1, 2, 3, 4 };
byte[] copy = original.clone();
for (byte b : copy) {
System.out.print(b + " ");
}
int[] originalInt = { 11, 22, 33, 44 };
int[] copyInt = originalInt.clone();
for (int i : copyInt) {
System.out.print(i + " ");
}
String[] originalStr = { "1", "2", "3", "4" };
String[] copyStr = originalStr.clone();
for (String s : copyStr) {
System.out.print(s + " ");
}
Main[] originalMain = { new Main() };
Main[] copyMain = originalMain.clone();
for (Main m : copyMain) {
System.out.print(m + " ");
}
}
}
Zecas source
share