Search code examples
javaarraysmethodstypesprimitive

Is java's array "primitive type"?


When I typed:

int[] i = new int[3];

in my IntelliJ and I can see that "i" has "length" property and "clone" method. So I really wonder, is java's "Array" a raw type or not? I suppose only "objects" should have property of methods right?

Or there's something special done by java compiler or jvm, that makes raw type of Array "just look like objects"?

Please kindly help to explain. Thanks!


Solution

  • Is java's array “primitive type”?

    No. Java's array is a reference type. You can see the java.lang.reflect.Array class for insight into how it works internally. For example,

    int[] i = { 1, 2, 3 };
    Object v = i;
    System.out.println(v.getClass());
    System.out.println(Array.get(v, 1));
    

    Outputs

    class [I
    2