Java Reference
In-Depth Information
When the statement
Supplier<Item> func1 = Item::new;
is executed, the compiler finds that the target type
Supplier<String>
does not accept any argument. Therefore, it uses the no-args constructor of the
Item
class.
When the statement
Function<String,Item> func2 = Item::new;
is executed, the compiler finds that the
target type
Function<String,Item>
takes a
String
argument. Therefore, it uses the constructor of the Item class that
takes a
String
argument.
When the statement
BiFunction<String,Double,Item> func3 = Item::new;
is executed, the compiler finds
that the target type
BiFunction<String,Double,Item>
takes two arguments: a
String
and a
Double
. Therefore, it uses
the constructor of the
Item
class that takes a
String
and a
double
argument.
The following statement generates a compile-time error, as the compiler does not find a constructor in the
Item
class that accepts a
Double
argument:
Function<Double,Item> func4 = Item::new; // A compile-time error
Arrays in Java do not have constructors. There is a special syntax to use constructor references for arrays. Array
constructors are treated to have one argument of
int
type that is the size of the array. The following snippet of code
shows the lambda expression and its equivalent constructor reference for an
int
array:
// Uses a lambda expression
IntFunction<int[]> arrayCreator1 = size -> new int[size];
int[] empIds1 = arrayCreator1.apply(5); // Creates an int array of five elements
// Uses an array constructor reference
IntFunction<int[]> arrayCreator2 = int[]::new;
int[] empIds2 = arrayCreator2.apply(5); // Creates an int array of five elements
You can also use a
Function<Integer,R>
type to use an array constructor reference, where
R
is the array type.
// Uses an array constructor reference
Function<Integer,int[]> arrayCreator3 = int[]::new;
int[] empIds3 = arrayCreator3.apply(5); // Creates an int array of five elements
The syntax for the constructor reference for arrays supports creating an array of multiple dimensions. However,
you can specify the length for only the first dimension. The following statement creates a two-dimensional
int
array
with the first dimension having the length of 5:
// Uses an array constructor reference
IntFunction<int[][]> TwoDimArrayCreator = int[][]::new;
int[][] matrix = TwoDimArrayCreator.apply(5); // Creates an int[5][] array
You might be tempted to use a
BiFunction<Integer,Integer,int[][]>
to use a constructor reference for a
two-dimensional array to supply the length for both dimensions. However, the syntax is not supported. Array
constructors are supposed to accept only one parameter that is the length of the first dimension. The following
statement generates a compile-time error:
BiFunction<Integer,Integer,int[][]> arrayCreator = int[][]::new;
Generic Method References
Typically, the compiler figures out the actual type for generic type parameters when a method reference refers to a
generic method. Consider the following generic method in the
Arrays
class in the
java.util
package:
static <T> List<T> asList(T... a)
•