Method References - Quiz
Total: 14 questions
1. What are method references?
What are method references?
Lambda expressions are used to create anonymous methods. In cases when a lambda expression only invokes one existing method, it is better to refer to the existing method by name. Method references gives the possibility to do this. They are compact lambda expressions for methods that already have a name.
2. Types of method references.
Types of method references.
There are four types of method references:
- Reference to a static method
- Reference to an instance method of a particular object
- Reference to an instance method of an arbitrary object of a particular type
- Reference to a constructor
3. Syntax of a reference to a static method.
Syntax of a reference to a static method.
ContainingClass::staticMethodName
4. Syntax of a reference to an instance method of a particular object.
Syntax of a reference to an instance method of a particular object.
containingObject::instanceMethodName
5. Syntax of a reference to an instance method of an arbitrary object of a particular type.
Syntax of a reference to an instance method of an arbitrary object of a particular type.
ContainingType::methodName
6. Syntax of a reference to a constructor.
Syntax of a reference to a constructor.
ClassName::new
7. Refactor example using method reference:
Function<String, Boolean> function = e -> Boolean.valueOf(e);
System.out.println(function.apply("TRUE"));
Refactor example using method reference:
Function<String, Boolean> function = e -> Boolean.valueOf(e);
System.out.println(function.apply("TRUE"));Function<String, Boolean> function = Boolean::valueOf;
System.out.println(function.apply("TRUE"));
8. Refactor an example:
IntFunction<String> function = i -> String.valueOf(i);
System.out.println(function.apply(450));
Refactor an example:
IntFunction<String> function = i -> String.valueOf(i);
System.out.println(function.apply(450));IntFunction<String> function = String::valueOf;
System.out.println(function.apply(450));
9. When reference to an instance method of a particular object can be used?
When reference to an instance method of a particular object can be used?
This type of method references is used when a lambda expression calls a method of an external object that already exists.
10. Refactor an example:
Consumer<String> consumer = e -> System.out.println(e);
consumer.accept("OCPJP 8");
Refactor an example:
Consumer<String> consumer = e -> System.out.println(e);
consumer.accept("OCPJP 8");Consumer<String> consumer = System.out::println;
consumer.accept("OCPJP 8");
11. Refactor an example:
Integer integer = new Integer(5);
Supplier<String> supplier = () -> integer.toString();
System.out.println(supplier.get());
Refactor an example:
Integer integer = new Integer(5);
Supplier<String> supplier = () -> integer.toString();
System.out.println(supplier.get());Integer integer = new Integer(5);
Supplier<String> supplier = integer::toString;
System.out.println(supplier.get());
12. Refactor example:
Function<String, String> function = s -> s.toLowerCase();
System.out.println(function.apply("OCPJP 8"));
Refactor example:
Function<String, String> function = s -> s.toLowerCase();
System.out.println(function.apply("OCPJP 8"));Function<String, String> function = String::toLowerCase;
System.out.println(function.apply("OCPJP 8"));
13. Refactor example:
Function<String, Integer> function = (d) -> new Integer(d);
System.out.println(function.apply("4"));
Refactor example:
Function<String, Integer> function = (d) -> new Integer(d);
System.out.println(function.apply("4"));Function<String, Integer> function = Integer::new;
System.out.println(function.apply("4"));
14. If a class declares multiple constructors, how the compiler choose one for constructor reference?
If a class declares multiple constructors, how the compiler choose one for constructor reference?
The compiler checks the type of the functional interface with all of the constructors and choose the best match.