3

For an ordinary class, I can write String.class.

But how do I do this for an array class, including array of primitives?

I can of course do as follows, whether for arrays of primitives or of objects.

(new byte[0]).getClass() and (new String[0]).getClass()

But is there a more direct way?

Joshua Fox
  • 18,704
  • 23
  • 87
  • 147
  • 3
    `String[].class`, `int[].class` – Eran Mar 27 '18 at 10:28
  • 1
    You may find [this](https://stackoverflow.com/questions/7011067/retrieving-array-class-name) and [this](https://stackoverflow.com/questions/13392160/about-java-get-string-class-from-string-class-what-if-string-class-is) interesting. – Federico klez Culloca Mar 27 '18 at 10:28

1 Answers1

3

The correct way is using the .class.

Examples:

int[].class
Long[].class

These will do exactly what you need.