Method References in Java

Dear Sciaku Learner you are not logged in or not enrolled in this course.

Please Click on login or enroll now button.

If you have any query feel free to chat us!

Happy Coding! Happy Learning!

Lecture 81:-  Method References in Java

Method references in Java provide a shorthand notation for referring to methods or constructors. They are a concise and readable way to pass methods as arguments to higher-order functions or to use them in functional interfaces. Method references make your code more expressive and allow you to reuse existing methods as lambda expressions.

Method references are typically used in the context of functional interfaces, where a single abstract method needs to be implemented. There are four types of method references:

  1. Reference to a Static Method:
    • Syntax: ClassName::staticMethodName
    • Example:

      javaCopy code

      // Using a method reference to refer to the static method Integer.parseInt() Function<String, Integer> parser = Integer::parseInt; int number = parser.apply("123"); // number = 123

  2. Reference to an Instance Method of a Particular Object:
    • Syntax: objectReference::instanceMethodName
    • Example:

      javaCopy code

      // Using a method reference to refer to the instance method of an object String str = "Hello, World!"; Consumer<String> printString = System.out::println; printString.accept(str); // Output: Hello, World!

  3. Reference to an Instance Method of an Arbitrary Object of a Particular Type:
    • Syntax: ClassName::instanceMethodName
    • Example:

      javaCopy code

      // Using a method reference to refer to the instance method of an object of a specific type List<String> names = Arrays.asList("Alice", "Bob", "Charlie"); names.forEach(String::toUpperCase);

  4. Reference to a Constructor:
    • Syntax: ClassName::new
    • Example:

      javaCopy code

      // Using a method reference to refer to a constructor Supplier<List<String>> listSupplier = ArrayList::new; List<String> newList = listSupplier.get();

In each type of method reference, the method signature (number and types of arguments) should match the functional interface's abstract method signature.

Method references can simplify your code when the lambda expression's sole purpose is to call an existing method with the same signature as the functional interface's abstract method. They are particularly useful when working with streams and functional programming constructs.

Remember that method references are not always a replacement for lambda expressions. They are an alternative when you have existing methods with compatible signatures. You can choose between lambda expressions and method references based on readability and code elegance.

12. Advanced

Comments: 0

Frequently Asked Questions (FAQs)

How do I register on Sciaku.com?
How can I enroll in a course on Sciaku.com?
Are there free courses available on Sciaku.com?
How do I purchase a paid course on Sciaku.com?
What payment methods are accepted on Sciaku.com?
How will I access the course content after purchasing a course?
How long do I have access to a purchased course on Sciaku.com?
How do I contact the admin for assistance or support?
Can I get a refund for a course I've purchased?
How does the admin grant access to a course after payment?