← Back to Advanced Java Course | Chapter 3: Functional Programming | Lesson 5 of 6

Java के Method References

एक method reference किसी lambda लिखने के बजाय किसी मौजूदा method की ओर इशारा करने का एक छोटा तरीका है, जैसे 'वह tool उपयोग करो' कहना बजाय उसे describe करने के। यह code को छोटा बनाता है।
Syntax
java
ClassName::staticMethod
object::instanceMethod
ClassName::instanceMethod
ClassName::new

Introduction to Method References

Method references सिर्फ किसी मौजूदा method को call करने वाले simple lambda expressions लिखने का एक shorthand syntax हैं। वे उस method को नाम से refer करने के लिए double colon (::) operator का उपयोग करते हैं बिना इसे तुरंत call किए।

उदाहरण: Introduction to Method References

java
import java.util.function.Consumer;
public class Main {
	public static void main(String[] args) {
		Consumer<String> printer = System.out::println; // shorthand for s -> System.out.println(s)
		printer.accept("Using ::");
	}
}

Reference to an Instance Method of a Particular Object

आप किसी एक specific, पहले से मौजूद object से संबंधित instance method को 'instance::instanceMethod' format का उपयोग करके refer कर सकते हैं। यह call को सीधे उसी particular object से बांध देता है, इसलिए reference का हर invocation उसी instance पर operate करता है।

उदाहरण: Reference to an Instance Method of a Particular Object

java
import java.util.function.Supplier;
public class Main {
	public static void main(String[] args) {
		String greeting = "Hello";
		Supplier<Integer> lengthGetter = greeting::length; // bound to this specific instance
		System.out.println(lengthGetter.get());
	}
}

Reference to an Instance Method of an Arbitrary Object

आप किसी दिए गए type के किसी arbitrary object के instance method को 'Type::instanceMethod' format का उपयोग करके भी refer कर सकते हैं। इस style में, functional interface को दिया गया पहला argument वह object बन जाता है जिस पर method असल में call होता है, बजाय किसी fixed instance के जिसे आपने पहले नाम दिया हो।

उदाहरण: Reference to an Instance Method of an Arbitrary Object

java
import java.util.function.Function;
public class Main {
	public static void main(String[] args) {
		Function<String, Integer> toLength = String::length; // first argument becomes the receiver
		System.out.println(toLength.apply("arbitrary"));
	}
}

Constructor References

आप 'Class::new' format का उपयोग करके constructors के references लिख सकते हैं, जो आपको objects को dynamically instantiate करने का एक साफ़, reusable तरीका देता है, उदाहरण के लिए किसी stream के collect() या map() call में पास किए गए Supplier के रूप में।

उदाहरण: Constructor References

java
// Import java.util.function.Supplier so it can be used by its short name
import java.util.function.Supplier;
// Import java.util.ArrayList so it can be used by its short name
import java.util.ArrayList;
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		Supplier<ArrayList<String>> listMaker = ArrayList::new;
		ArrayList<String> list = listMaker.get();
		list.add("created via constructor reference");
		// Print a line to the console
		System.out.println(list);
	}
}

Method References vs Lambdas

जब भी आप बिना बदले किसी मौजूदा method को सिर्फ forward कर रहे हों तो method references lambdas से ज़्यादा संक्षिप्त रूप से पढ़े जाते हैं। लेकिन अगर आपको arguments transform करने हों, अतिरिक्त logic जोड़ना हो, या कई steps साथ combine करने हों, तो एक standard lambda expression बेहतर, ज़्यादा flexible choice है।

उदाहरण: Method References vs Lambdas

java
import java.util.function.Function;
public class Main {
	public static void main(String[] args) {
		Function<String, Integer> ref = String::length; // simple forward, reads cleanly
		Function<String, Integer> lambda = s -> s.length() + 1; // extra logic needs a lambda
		System.out.println(ref.apply("abc") + " " + lambda.apply("abc"));
	}
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. parentheses के साथ String::length() लिखना, जो एक syntax error है; किसी method reference में कोई parentheses या arguments नहीं होते।
  2. ऐसे method reference का उपयोग करना जिसके parameters functional interface से मेल न खाएं, जो 'invalid method reference' जैसा compile error देता है।
  3. किसी Function<Integer, String> के लिए Integer::toString का उपयोग करना, जो static और instance versions के बीच अस्पष्ट है और compile नहीं होता; इसके बजाय एक lambda का उपयोग करें।
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.