How do Java 8 array constructor references work? How do Java 8 array constructor references work? arrays arrays

How do Java 8 array constructor references work?


You can find out yourself by decompiling the java bytecode:

javap -c -v -p MyClass.class

The compiler desugars array constructor references Foo[]::new to a lambda (i -> new Foo[i]), and then proceeds as with any other lambda or method reference. Here's the disassembled bytecode of this synthetic lambda:

private static java.lang.Object lambda$MR$new$new$635084e0$1(int);descriptor: (I)Ljava/lang/Object;flags: ACC_PRIVATE, ACC_STATIC, ACC_SYNTHETICCode:  stack=1, locals=1, args_size=1     0: iload_0            1: anewarray     #6                  // class java/lang/String     4: areturn       

(It's return type is Object because the erased return type in IntFunction is Object.)