

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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?
A method reference is similar to lambda expression used to refer a method without invoking it while constructor reference used to refer to the constructor without instantiating the named class. A method reference requires a target type similar to lambda expressions. But instead of providing an implementation of a method, they refer to a method of an existing class or object while a constructor reference provides a different name to different constructors within a class.
syntax - Method Reference
<Class-Name>::<instanceMethodName>
Example
import java.util.*; public class MethodReferenceTest { public static void main(String[] args) { List<String> names = new ArrayList<String>(); List<String> selectedNames = new ArrayList<String>(); names.add("Adithya"); names.add("Jai"); names.add("Raja"); names.forEach(selectedNames :: add); // instance method reference System.out.println("Selected Names:"); selectedNames.forEach(System.out :: println); } }
Output
Selected Names: Adithya Jai Raja
Syntax -Constructor Reference
<Class-Name>::new
Example
@FunctionalInterface interface MyInterface { public Student get(String str); } class Student { private String str; public Student(String str) { this.str = str; System.out.println("The name of the student is: " + str); } } public class ConstrutorReferenceTest { public static void main(String[] args) { MyInterface constructorRef = Student :: new; // constructor reference constructorRef.get("Adithya"); } }
Output
The name of the student is: Adithya
- Related Questions & Answers
- Differences between Lambda Expression and Method Reference in Java?
- Reference to a constructor using method references in Java8
- Difference Between Pointer and Reference
- Differences between pass by value and pass by reference in C++
- How to create a constructor reference for an array in Java?
- What is the difference between object and reference in java?
- Difference between constructor and method in Java
- What are the differences between default constructor and parameterized constructor in Java?
- How to implement DoubleBinaryOperator using lambda and method reference in Java?
- How to implement LongBinaryOperator using lambda and method reference in Java?
- How to implement DoublePredicate using lambda and method reference in Java?
- How to implement IntConsumer using lambda and method reference in Java?
- How to implement LongSupplier using lambda and method reference in Java?
- How to implement DoubleUnaryOperator using lambda and method reference in Java?
- How to implement DoubleSupplier using lambda and method reference in Java?
Advertisements