Should Object.clone () not require an explicit cast?

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 + " ");
        }
    } // end method main

} // end class Main
+3
source share
1 answer

You do not call Object.clone(). You call T[].clone(), which is redefined to return T[].

JLS 10.7 Members of the array :

Array type elements are as follows:

  • clone, Object . clone T[] T[].
+5

All Articles