Method References in Java
What are Method References?
In Java, method references provide a way to refer to methods or constructors using concise lambda-like syntax.
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
Example 1: Reference to a Static Method
// Static method
class MathUtils {
public static int square(int num) {
return num * num;
}
}
// Method reference
Function<Integer, Integer> squareFunction = MathUtils::square;
int result = squareFunction.apply(5); // result = 25
Example 2: Reference to an Instance Method of a Particular Object
// Class with an instance method
class Printer {
public void print(String message) {
System.out.println(message);
}
}
// Method reference
Printer printer = new Printer();
Consumer<String> printFunction = printer::print;
printFunction.accept("Hello, World!"); // prints "Hello, World!"
Example 3: Reference to an Instance Method of an Arbitrary Object of a Particular Type
// Class with an instance method
class StringFormatter {
public String format(String str) {
return str.toUpperCase();
}
}
// Method reference
Function<String, String> formatFunction = StringFormatter::format;
String result = formatFunction.apply("hello"); // result = "HELLO"
Example 4: Reference to a Constructor
// Class with a constructor
class Person {
private String name;
public Person(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
// Method reference
Function<String, Person> personConstructor = Person::new;
Person person = personConstructor.apply("John");
String name = person.getName(); // name = "John"
Example 5: Reference to an Instance Method of an Existing Object
// Class with an instance method
class StringUtil {
public int getLength(String str) {
return str.length();
}
}
// Method reference
StringUtil stringUtil = new StringUtil();
Function<String, Integer> lengthFunction = stringUtil::getLength;
int length = lengthFunction.apply("Hello"); // length = 5
Benefits of Method References
- They provide a more concise and readable alternative to lambda expressions.
- They can improve code maintainability by promoting code reuse.
- They enable a functional programming style in Java.
Method References | Lambda Expressions |
---|---|
Method References are a shorthand syntax for invoking a method using the double colon (::) operator. | Lambda Expressions are anonymous functions that can be used as arguments for functional interfaces. |
Method References provide a way to reuse existing methods as lambdas, making the code more concise and readable. | Lambda Expressions allow the creation of inline implementations of functional interfaces, providing more flexibility and custom logic. |
Method References can only be used to refer to existing methods, including static methods, instance methods, and constructors. | Lambda Expressions can be used to define inline implementations of functional interfaces, with custom logic and parameter handling. |
Method References are often used when the lambda expression is a simple method call without additional logic. | Lambda Expressions are useful when more complex logic needs to be implemented inline, especially for one-time use. |
Example:
|
Example:
|
In the above examples, the Method Reference `System.out::println` is used to refer to the existing method `println` of the `System.out` object, while the Lambda Expression `name -> System.out.println(name)` defines an inline implementation of the `Consumer` functional interface to print each name in the list.
Tags
Java Method References
Method References in Java
Method References vs. Lambda Expressions in Java
Static Method References in Java
Types of Method References in Java