Java का Ternary Operator
In this page:
result = condition ? valueIfTrue : valueIfFalse;
Introduction to Ternary
Ternary operator if (cond) { x = a; } else { x = b; } को एक expression में condense करता है, x = cond ? a : b, condition evaluate करते हुए और result के आधार पर colon से पहले या बाद वाली value चुनते हुए।
उदाहरण: Introduction to Ternary
public class Main {
public static void main(String[] args) {
int age = 20;
String result = age >= 18 ? "adult" : "minor"; // condenses if/else into one expression
System.out.println(result);
}
}
Login to try C/C++/Java/PHP code in the editor
Setting Values with Ternary
चूंकि ternary operator खुद एक value produce करता है, आप इसका result एक line में सीधे एक variable को assign कर सकते हैं बजाय पहले variable declare करने और एक अलग if-else block में इसे भरने के।
उदाहरण: Setting Values with Ternary
public class Main {
public static void main(String[] args) {
int score = 75;
String grade = score >= 60 ? "Pass" : "Fail"; // assigned directly in one line
System.out.println(grade);
}
}
Login to try C/C++/Java/PHP code in the editor
Nested Ternary Operator
एक ternary को किसी दूसरे के अंदर nest करना, जैसे a ? 1 : (b ? 2 : 3), आपको एक single expression में तीन या ज़्यादा branches express करने देता है, लेकिन हर जोड़ा level line को एक नज़र में parse करना ज़्यादा मुश्किल बनाता है -- ज़्यादातर style guides nesting को एक level पर cap करते हैं।
उदाहरण: Nested Ternary Operator
public class Main {
public static void main(String[] args) {
int score = 85;
String grade = score >= 90 ? "A" : (score >= 80 ? "B" : "C"); // three branches in one expression
System.out.println(grade);
}
}
Login to try C/C++/Java/PHP code in the editor
Direct Printing with Ternary
आप एक ternary expression को println() को एक argument के रूप में सीधे embed कर सकते हैं, सिर्फ string चुनने के लिए एक अलग if-else block लिखे बिना एक condition के आधार पर अलग printed text produce करते हुए।
उदाहरण: Direct Printing with Ternary
public class Main {
public static void main(String[] args) {
int stock = 0;
System.out.println(stock > 0 ? "In Stock" : "Out of Stock"); // embedded directly in println
}
}
Login to try C/C++/Java/PHP code in the editor
Ternary vs If-Else
Ternary simple, single-value decisions के लिए चमकता है, लेकिन एक बार आपको per branch कई statements या side effects चाहिए, एक पूरा if-else block एक line में logic ठूंसने से clearer और debug करना आसान है।
उदाहरण: Ternary vs If-Else
public class Main {
public static void main(String[] args) {
int age = 20;
String label = age >= 18 ? "adult" : "minor"; // simple, single-value: ternary is clear
if (age >= 18) { // multiple statements needed: if-else is clearer here
System.out.println("Granting access");
System.out.println(label);
}
}
}
Login to try C/C++/Java/PHP code in the editor
- Result types mix करना, जैसे
cond ? 1 : "one", जो एकintvariable को assign नहीं किया जा सकता। - कई ternaries nest करना, जो code पढ़ना मुश्किल बनाता है।
- एक ternary को एक statement के रूप में उपयोग करना, जैसे
x > 0 ? print("a") : print("b");, जो एक compile error है क्योंकि इसे एक value produce करनी चाहिए।
Chapter Quiz — Complete all 9 topics to unlock
0/9 topics done
Complete these topics first: