2

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!

Hind Forsum
  • 9,717
  • 13
  • 63
  • 119
  • "Raw type" means something completely unrelated to what you're talking about. – user2357112 Dec 24 '18 at 03:36
  • +1. The question may seem strange on its own, but it makes more sense if you know that the OP has recently been told, on this very site, that arrays *are* primitives: [Why java List has “List.toArray()” but Array needs static function of “Array.toList()”?](https://stackoverflow.com/q/53908648/978917). – ruakh Dec 24 '18 at 04:11

1 Answers1

3

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
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249