← Back to Advanced Java Course | Chapter 4: Modern Java Features | Lesson 12 of 12

Java के Record Patterns

Record patterns, जो Java 21 में finalized हुए, किसी record के components को सीधे instanceof या switch के अंदर destructure करते हैं, और nest हो सकते हैं, var उपयोग कर सकते हैं, और exhaustive matching के लिए sealed interfaces के साथ combine हो सकते हैं।
Syntax
java
record RecordName(Type a, Type b) { }

if (object instanceof RecordName(Type a, Type b)) {
    // a and b are extracted
}

What are Record Patterns?

Record patterns, जो Java 21 में finalized हुए, किसी record को सीधे किसी instanceof या switch के अंदर destructure करने देते हैं, उसके components को उसी expression में नामित variables में निकालते हुए बजाय हर accessor method को अलग से call करने के।

उदाहरण: What are Record Patterns?

java
public class Main {
	record Point(int x, int y) {}
	public static void main(String[] args) {
		Object obj = new Point(3, 4);
		if (obj instanceof Point(int x, int y)) { // deconstructed in the same expression
			System.out.println(x + "," + y);
		}
	}
}

Basic Record Deconstruction

किसी Point से destructured components जैसे x और y उसी condition के अंदर normal local variables की तरह तुरंत उपयोग किए जा सकते हैं, pattern check के साथ combined आगे के boolean expressions में भी।

उदाहरण: Basic Record Deconstruction

java
public class Main {
	record Point(int x, int y) {}
	public static void main(String[] args) {
		Object obj = new Point(5, 10);
		if (obj instanceof Point(int x, int y) && x > 0) { // usable in further conditions
			System.out.println("Positive x: " + x);
		}
	}
}

Nested Record Patterns

Record patterns nest हो सकते हैं, एक ऐसे record को destructure करते हुए जो खुद दूसरे records रखता है एक single pattern में, manual accessor calls की chain के बिना गहराई से nested structures तक सीधे पहुंचते हुए।

उदाहरण: Nested Record Patterns

java
public class Main {
	record Point(int x, int y) {}
	record Line(Point start, Point end) {}
	public static void main(String[] args) {
		Object obj = new Line(new Point(0, 0), new Point(5, 5));
		if (obj instanceof Line(Point(int x1, int y1), Point(int x2, int y2))) { // nested deconstruction
			System.out.println(x1 + "," + y1 + " -> " + x2 + "," + y2);
		}
	}
}

Record Patterns with var

var keyword का उपयोग किसी record pattern के किसी भी component के लिए explicit type की जगह किया जा सकता है, compiler को record की अपनी definition से type infer करने देते हुए और pattern को छोटा रखते हुए।

उदाहरण: Record Patterns with var

java
public class Main {
	record Point(int x, int y) {}
	public static void main(String[] args) {
		Object obj = new Point(7, 8);
		if (obj instanceof Point(var x, var y)) { // type inferred from the record definition
			System.out.println(x + "," + y);
		}
	}
}

Record Patterns in Switch

Record patterns किसी sealed hierarchy पर switch expressions के साथ मिलकर खासतौर पर अच्छे से काम करते हैं, हर संभव record type को उसके अपने case branch में destructure करते हुए, और एक when clause किसी specific case में अतिरिक्त guard conditions जोड़ सकता है।

उदाहरण: Record Patterns in Switch

java
// Define the class Main
public class Main {
	sealed interface Shape permits Circle, Square {}
	// Define the record Circle (an immutable data carrier)
	record Circle(double radius) implements Shape {}
	// Define the record Square (an immutable data carrier)
	record Square(double side) implements Shape {}
	static String describe(Shape s) {
		// Send switch (s) { back to the caller
		return switch (s) {
			case Circle(double r) when r > 10 -> "Big circle";
			case Circle(double r) -> "Circle r=" + r;
			case Square(double side) -> "Square side=" + side;
		};
	}
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		// Print a line to the console
		System.out.println(describe(new Circle(5)));
	}
}
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. गलत संख्या में components वाला pattern लिखना, जैसे दो-component record के लिए Point(int x), जो एक compile error है।
  2. किसी component के लिए गलत type का उपयोग करना, जैसे record Point(int x, int y) के लिए Point(String x, int y), जो compile नहीं होता।
  3. किसी normal class को record pattern से destructure करने की कोशिश करना, जो सिर्फ records के लिए काम करता है।
चैप्टर सारांश
  • Java 8 features ने lambdas और streams introduce किए, और बाद के releases ने var, records, और sealed classes जोड़े।
  • Pattern matching, text blocks, switch expressions, और record patterns code को छोटा और साफ़ बनाते हैं।
  • Module system, virtual threads, structured concurrency, और sequenced collections बड़े applications और concurrency को address करते हैं।

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.