Variable parameter class?

Following code

    public static void main(String[] args) {
        fun(new Integer(1));
    }
    static void fun(Object ... a) {
        System.out.println(a.getClass());
    }

gives the result: -

class [Ljava.lang.Object;

What is this class?

+3
source share
3 answers

an array Object[].

To get runtime type information:

a.getClass().isArray() -> true
a.getClass().getComponentType().getName() -> java.lang.Object
+7
source

according to the JVM specification , it is just an java.lang.Object array :

  • [ means monomeric array
  • LfullyQualifiedName; means class L; it's just the syntax
+6
source

varargs ( ) Java - varargs .

+1

Source: https://habr.com/ru/post/1718783/


All Articles