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

Java के Wildcards

एक wildcard एक question mark है जिसका मतलब है 'कोई भी type, मुझे फर्क नहीं पड़ता', जैसे किसी भी तरह का fruit basket accept करना। आप इसे types के एक परिवार तक सीमित कर सकते हैं।
Syntax
java
List<?> list                        // unknown type
List<? extends UpperBound> list     // read-only producer
List<? super LowerBound> list       // consumer

What is a Wildcard?

Generic code में, question mark (?) को wildcard कहा जाता है, और यह एक fixed type parameter के बजाय एक अज्ञात type parameter को represent करता है। Wildcards एक single method signature को callers को एक exact type में बंद करने के बजाय अलग-अलग, संबंधित parameter types की collections accept करने देते हैं।

उदाहरण: What is a Wildcard?

java
// Import java.util.List so it can be used by its short name
import java.util.List;
// Define the class Main
public class Main {
	static void printSize(List<?> list) {
		// Print a line to the console
		System.out.println("Size: " + list.size());
	}
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		printSize(List.of("a", "b", "c"));
		printSize(List.of(1, 2));
	}
}

Unbounded Wildcards

एक unbounded wildcard एक single question mark से लिखा जाता है, जैसे List<?>, और यह किसी भी type parameter को represent करता है। यह उन methods के लिए सबसे उपयोगी है जिन्हें सिर्फ किसी collection के ज्ञात Object-level API से पढ़ना हो, जैसे उसका size print करना, elements असल में क्या हैं इसकी परवाह किए बिना।

उदाहरण: Unbounded Wildcards

java
// Import java.util.List so it can be used by its short name
import java.util.List;
// Define the class Main
public class Main {
	static void printAll(List<?> list) {
		// Print a line to the console
		System.out.println("Items: " + list.size());
	}
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		printAll(List.of(1, 2, 3));
	}
}

Upper Bounded Wildcards

एक upper bounded wildcard type parameter को किसी specific class या उसकी subclasses तक सीमित करता है, जिसे extends keyword से लिखा जाता है, उदाहरण के लिए <? extends Number>। यह किसी method को उस collection से values को उस upper-bound type के रूप में सुरक्षित रूप से पढ़ने देता है, क्योंकि हर संभव element की गारंटी है कि वह कम से कम उस type का होगा।

उदाहरण: Upper Bounded Wildcards

java
// Import java.util.List so it can be used by its short name
import java.util.List;
// Define the class Main
public class Main {
	static double sum(List<? extends Number> nums) {
		// Declare total and set it to 0
		double total = 0;
		for (Number n : nums) total += n.doubleValue();
		// Send total back to the caller
		return total;
	}
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		// Print a line to the console
		System.out.println(sum(List.of(1, 2, 3)));
	}
}

Lower Bounded Wildcards

एक lower bounded wildcard type parameter को किसी specific class या उसकी superclasses तक सीमित करता है, जिसे super keyword से लिखा जाता है, उदाहरण के लिए <? super Integer>। यह किसी method को उस type की values को collection में सुरक्षित रूप से लिखने देता है, क्योंकि किसी भी superclass container की गारंटी है कि वह उन्हें रख सकेगा।

उदाहरण: Lower Bounded Wildcards

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 void addNumbers(List<? super Integer> list) {
		list.add(10);
		list.add(20);
	}
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		// Create a new ArrayList object and store it in numbers
		List<Number> numbers = new ArrayList<>();
		addNumbers(numbers);
		// Print a line to the console
		System.out.println(numbers);
	}
}

Wildcard Guidelines (PECS)

PECS mnemonic संक्षेप में बताता है कि हर एक का उपयोग कब करें: Producer Extends, Consumer Super। एक extends wildcard का उपयोग करें जब कोई collection सिर्फ वे values produce करती हो जिन्हें आप पढ़ रहे हैं, और एक super wildcard का उपयोग करें जब कोई collection सिर्फ वे values consume करती हो जिन्हें आप उसमें डाल रहे हैं।

उदाहरण: Wildcard Guidelines (PECS)

java
import java.util.List;
public class Main {
	static double sum(List<? extends Number> producer) { // Producer Extends
		double total = 0;
		for (Number n : producer) total += n.doubleValue();
		return total;
	}
	static void fill(List<? super Integer> consumer) { // Consumer Super
		consumer.add(1);
	}
	public static void main(String[] args) {
		System.out.println(sum(List.of(1, 2, 3)));
	}
}
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. किसी List<?> या List<? extends Number> पर list.add(5) call करने की कोशिश करना, जो compile नहीं होता क्योंकि compiler exact element type नहीं जान सकता (सिर्फ null जोड़ा जा सकता है)।
  2. extends और super को मिला देना: किसी collection से पढ़ने के लिए ? extends T और उसमें लिखने के लिए ? super T (PECS) का उपयोग करें।
  3. यह सोचना कि List<?> और List<Object> एक जैसे हैं, जबकि एक List<String> को List<?> में पास किया जा सकता है लेकिन List<Object> में नहीं।
चैप्टर सारांश
  • Generics type safety बनाए रखते हुए classes और methods को अलग-अलग types के साथ काम करने देते हैं।
  • Generic classes और generic methods एक बार लिखे जाते हैं और कई types के लिए reuse होते हैं।
  • Wildcards तब flexibility देते हैं जब किसी method को कई संबंधित generic types accept करने होते हैं।
🔒

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.