How do Java 8 array constructor references work?

拈花ヽ惹草 提交于 2019-11-28 20:04:46

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_SYNTHETIC
Code:
  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.)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!