← Back to Core Java Course | Chapter 3: Operators | Lesson 8 of 9

Java का Ternary Operator

Ternary operator एक सवाल के आधार पर दो answers में से चुनने का एक-line तरीका है, जैसे 'अगर बारिश हो umbrella लो, नहीं तो sunglasses'। यह एक छोटा if-else है।
Syntax
java
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

java
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);
	}
}

Setting Values with Ternary

चूंकि ternary operator खुद एक value produce करता है, आप इसका result एक line में सीधे एक variable को assign कर सकते हैं बजाय पहले variable declare करने और एक अलग if-else block में इसे भरने के।

उदाहरण: Setting Values with Ternary

java
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);
	}
}

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

java
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);
	}
}

Direct Printing with Ternary

आप एक ternary expression को println() को एक argument के रूप में सीधे embed कर सकते हैं, सिर्फ string चुनने के लिए एक अलग if-else block लिखे बिना एक condition के आधार पर अलग printed text produce करते हुए।

उदाहरण: Direct Printing with Ternary

java
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
	}
}

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

java
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);
		}
	}
}
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. Result types mix करना, जैसे cond ? 1 : "one", जो एक int variable को assign नहीं किया जा सकता।
  2. कई ternaries nest करना, जो code पढ़ना मुश्किल बनाता है।
  3. एक ternary को एक statement के रूप में उपयोग करना, जैसे x > 0 ? print("a") : print("b");, जो एक compile error है क्योंकि इसे एक value produce करनी चाहिए।

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.