Selft training repo
Shorthand syntax for referring to an existing method by its name. They provide a concise way to pass a method as an argument or to assign a method to a functional interface.
Method references can be seen as a more compact alternative to lambda expressions when the lambda expression simply calls an existing method without any additional logic.
The syntax for method references in Java is as follows:
ClassName::methodName
There are several types of method references in Java:
Reference to a static method:
ClassName::staticMethodName
Reference to an instance method of a particular object:
instance::instanceMethodName
Reference to an instance method of an arbitrary object of a particular type:
ClassName::instanceMethodName
Reference to a constructor:
ClassName::new
Capitalizing and printing a list of Strings:
List<String> messages = Arrays.asList("hello", "baeldung", "readers!");
We can achieve this by leveraging a simple lambda expression calling the StringUtils.capitalize()
method directly:
messages.forEach(word -> StringUtils.capitalize(word));
Or, we can use a method reference to simply refer to the capitalize static method:
messages.forEach(StringUtils::capitalize);
Get Started | Languages | Java Development | Java 8 | PF in Java