← Back to Advanced Java Course | Chapter 1: Generics | Lesson 3 of 4

Java के Generic Methods

एक generic method एक ऐसा method है जो type को खाली छोड़कर कई types के साथ काम करता है, जैसे एक मशीन जो किसी भी shape के gift को wrap कर सकती है। Java आपके दिए हुए से type का पता लगा लेता है।
Syntax
java
public <T> returnType methodName(T parameter) {
    // body
}

methodName(argument);

What is a Generic Method?

एक generic method अपने ही signature में अपने खुद के अलग type parameters declare करता है, enclosing class के किसी type parameters से अलग। यह एक single method को किसी भी data type को accept और उसके साथ काम करने देता है, भले ही एक class के अंदर जो generic बिल्कुल नहीं है।

उदाहरण: What is a Generic Method?

java
// Define the class Main
public class Main {
	static <T> void show(T item) {
		// Print a line to the console
		System.out.println("Value: " + item);
	}
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		show("text");
		show(42);
	}
}

Declaring Type Parameters

एक generic method define करने के लिए, declaration line में method के return type से ठीक पहले अपने type parameters को angle brackets में रखें, जैसे <T>। यह compiler को बताता है कि parameter सिर्फ उस एक method call तक सीमित है, पूरी class तक नहीं।

उदाहरण: Declaring Type Parameters

java
// Define the class Main
public class Main {
	static <T> T identity(T value) {
		// Send value back to the caller
		return value;
	}
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		// Print a line to the console
		System.out.println(identity("scoped to this method"));
	}
}

Bounded Type Methods

आप bounded types का उपयोग करके किसी generic method के parameters को सीमित कर सकते हैं, यह सुनिश्चित करते हुए कि method सिर्फ किसी specific class की subclasses या किसी specific interface के implementers को accept करे। यह खासतौर पर तब उपयोगी है जब method के body को किसी ऐसे method को call करना हो जो सिर्फ उस bound पर मौजूद हो, जैसे Comparable पर compareTo()।

उदाहरण: Bounded Type Methods

java
// Define the class Main
public class Main {
	static <T extends Comparable<T>> T max(T a, T b) {
		// Send a.compareTo(b) > 0 ? a : b back to the caller
		return a.compareTo(b) > 0 ? a : b;
	}
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		// Print a line to the console
		System.out.println(max(5, 9));
	}
}

Multiple Method Parameters

Generic methods एक साथ कई type parameters declare कर सकती हैं, angle brackets के अंदर commas से अलग करते हुए, जैसे <T, U>, जब method को दो अलग-अलग input types को एक-दूसरे से जोड़ना हो। एक classic उदाहरण है ऐसा method जो एक typed list से elements को एक अलग-typed destination list में copy करता है।

उदाहरण: Multiple Method Parameters

java
// Import java.util.List so it can be used by its short name
import java.util.List;
// Import java.util.ArrayList so it can be used by its short name
import java.util.ArrayList;
// Define the class Main
public class Main {
	static <T, U> void copyLabeled(List<T> source, List<U> dest, U label) {
		for (T item : source) dest.add(label);
	}
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		List<Integer> nums = List.of(1, 2, 3);
		// Create a new ArrayList object and store it in labels
		List<String> labels = new ArrayList<>();
		copyLabeled(nums, labels, "n");
		// Print a line to the console
		System.out.println(labels);
	}
}

Type Inference

ज़्यादातर generic methods को call करते समय आपको explicitly type parameters specify करने की ज़रूरत नहीं है, क्योंकि Java compiler आपके पास किए arguments से data type को अपने आप infer कर लेता है। यह type inference ही generic method calls को साधारण method calls जितना साफ़ रखता है।

उदाहरण: Type Inference

java
public class Main {
	static <T> T pick(T a, T b) {
		return a;
	}
	public static void main(String[] args) {
		String result = pick("first", "second"); // no <String> needed, inferred
		System.out.println(result);
	}
}
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. return type से पहले <T> declare करना भूल जाना, जैसे static void show(T item), जो T के लिए 'cannot find symbol' error के साथ fail हो जाता है।
  2. किसी unbounded T पर a.compareTo(b) call करना, जो compile नहीं होता; इसे उपयोग करने के लिए <T extends Comparable<T>> declare करें।
  3. T[] लेने वाले method को int[] पास करना, जो काम नहीं करता क्योंकि T को reference type होना चाहिए; Integer[] का उपयोग करें।
🔒

Chapter Quiz — Complete all 4 topics to unlock

0/4 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.