In Java we may create IntFunction<String[]>
from 1D array constructor reference:
// both do the same thing
IntFunction<String[]> createArrayL = size -> new String[size];
IntFunction<String[]> createArrayMR = String[]::new;
Now I wonder why we cannot do this with a 2D array:
BiFunction<Integer, Integer, String[][]> createArray2DL =
(rows, cols) -> new String[rows][cols];
// error:
BiFunction<Integer, Integer, String[][]> createArray2DMR =
String[][]::new;
Of course we may write:
IntFunction<String[][]> createArray2DInvalidL = String[][]::new;
System.out.println(createArray2DInvalidL.apply(3)[0]); // prints null
but this will behave differently than:
new String[3][3]
because row arrays will not be initialized.
So my question is: why String[][]::new
doesn't work for 2D arrays (for me it looks like an inconsistency in language design)?