
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Differences between Method Reference and Constructor Reference in Java?
The Method Reference and Constructor Reference are part of Java 8's functional programming features, they used for refering to methods and constructors without executing them. They are often used in conjunction with functional interfaces, such as those defined in the java.util.function
package.
Method Reference
A method reference is a shorthand representation of a lambda expression for calling a method. A method reference refers to a method without executing it. Method references can refer to static methods, instance methods, and constructors.
Constructor Reference
A constructor reference is a unique kind of method reference that is a reference to a constructor. A constructor reference is used for creating objects without calling the constructor explicitly. They can be applied to Functional Interfaces that has one abstract method.
Method Reference vs Constructor Reference
Following is a comparison table between method reference and constructor reference:
Aspect | Method Reference | Constructor Reference |
---|---|---|
Definition | A reference to an existing method | A reference to a constructor of a class |
Syntax | ClassName::methodName |
ClassName::new |
Usage | Used to refer to static or instance methods | Used to create new instances of a class |
Parameters | Accepts parameters based on the method signature | Accepts parameters based on the constructor signature |
Return Type | Returns the result of the method call | Returns a new instance of the class |
Common Use Cases | Filtering, mapping, or performing actions on streams without writing lambda expressions | Creating new objects in a functional style, especially with streams or functional interfaces |
Lambda Expression Equivalent | (args) -> ClassName.methodName(args) |
(args) -> new ClassName(args) |
Example of Method Reference
Following is an example of method reference:
import java.util.Arrays; import java.util.List; import java.util.function.Consumer; public class MethodRef{ public static void main(String[] args) { List<String> names = Arrays.asList("Alice", "Bob", "Charlie"); // Using method reference to print each name Consumer<String> printName = System.out::println; names.forEach(printName); } }
Following is the output of the above code:
Alice Bob Charlie
Example of Constructor Reference
Following is an example of constructor reference:
import java.util.function.Supplier; public class ConstructorRef { public static void main(String[] args) { // Using constructor reference to create a new instance of StringBuilder Supplier<StringBuilder> stringBuilderSupplier = StringBuilder::new; StringBuilder sb = stringBuilderSupplier.get(); sb.append("Hello, World!"); System.out.println(sb.toString()); } }
Following is the output of the above code:
Hello, World!
In conclusion, method references and constructor references are features of Java 8. They enhance code readability and help get rid of boilerplate code, which makes it easy to use Java 8's concepts of functional programming.