Java के Record Patterns
In this page:
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?
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);
}
}
}
Login to try C/C++/Java/PHP code in the editor
Basic Record Deconstruction
किसी Point से destructured components जैसे x और y उसी condition के अंदर normal local variables की तरह तुरंत उपयोग किए जा सकते हैं, pattern check के साथ combined आगे के boolean expressions में भी।
उदाहरण: Basic Record Deconstruction
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);
}
}
}
Login to try C/C++/Java/PHP code in the editor
Nested Record Patterns
Record patterns nest हो सकते हैं, एक ऐसे record को destructure करते हुए जो खुद दूसरे records रखता है एक single pattern में, manual accessor calls की chain के बिना गहराई से nested structures तक सीधे पहुंचते हुए।
उदाहरण: Nested Record Patterns
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);
}
}
}
Login to try C/C++/Java/PHP code in the editor
Record Patterns with var
var keyword का उपयोग किसी record pattern के किसी भी component के लिए explicit type की जगह किया जा सकता है, compiler को record की अपनी definition से type infer करने देते हुए और pattern को छोटा रखते हुए।
उदाहरण: Record Patterns with var
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);
}
}
}
Login to try C/C++/Java/PHP code in the editor
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
// 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)));
}
}
Login to try C/C++/Java/PHP code in the editor
- गलत संख्या में components वाला pattern लिखना, जैसे दो-component record के लिए
Point(int x), जो एक compile error है। - किसी component के लिए गलत type का उपयोग करना, जैसे
record Point(int x, int y)के लिएPoint(String x, int y), जो compile नहीं होता। - किसी 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 करते हैं।
Chapter Quiz — Complete all 12 topics to unlock
0/12 topics done
Complete these topics first: