switch Statement in Java: Primitive, String, Enum Support Notes in Hindi & English : जावा switch स्टेटमेंट: int, char, String (Java 7) और enum और 20 महत्वपूर्ण MCQ प्रश्न (Rajasthan IT Exam)

Post a Comment

PGDCA, BCA, B-Tech, MCA, M-Tech, B.Sc computer science राजस्थान सूचना सहायक व कंप्यूटर शिक्षक विशेष नोट्स: इस अध्याय में हम switch Statement in Java: Primitive, String, Enum Support (जावा switch स्टेटमेंट: int, char, String (Java 7) और enum) की संपूर्ण थ्योरी, मुख्य तकनीकी बिंदु, तुलनात्मक तालिका और विगत परीक्षाओं के पैटर्न पर आधारित 20 अति-महत्वपूर्ण बहुविकल्पीय प्रश्न (MCQs) विस्तृत हिंदी व अंग्रेजी व्याख्या सहित पढ़ेंगे।

Overview: Java's switch statement provides multi-way constant dispatching for integral primitives (byte, short, char, int), their wrapper classes, enums (since Java 5), and Strings (since Java 7), while rejecting long, float, double, and boolean. Traditional switch blocks fall through sequentially unless terminated by break, whereas Java 14 introduced arrow (->) switch expressions that return values safely without fall-through.

अध्याय सार: जावा का switch स्टेटमेंट पूर्णांक प्रिमिटिव्स (byte, short, char, int), उनकी रैपर क्लासेस, enum (Java 5 से) और String (Java 7 से) के लिए बहु-मार्गी डिस्पैचिंग प्रदान करता है, जबकि long, float, double और boolean को अस्वीकार करता है। पारंपरिक switch में break न होने पर फॉल-थ्रू होता है, वहीं Java 14 ने बिना फॉल-थ्रू वाले एरो (->) switch एक्सप्रेशन जोड़े।

switch Statement in Java: Primitive, String, Enum Support: Detailed Notes & Architecture (जावा switch स्टेटमेंट: int, char, String (Java 7) और enum)

1. Supported vs Unsupported Data Types in Java switch (जावा switch में समर्थित बनाम असमर्थित डेटा प्रकार)

[EN] A traditional Java switch expression accepts byte, short, char, and int, their auto-unboxed wrappers (Byte, Short, Character, Integer), enum types (added in Java 5), and java.lang.String (added in Java 7). Conversely, Java explicitly forbids switch on long, float, double, and boolean—using any of those four types in switch(...) produces an immediate compile-time error. Every 'case' label must be a compile-time constant expression (literals or initialized 'final' primitives/Strings) that fits within the switched type's range, and duplicate case constants are rejected by javac. Notice that when switching on an 'enum', case labels must use unqualified constant names such as 'case MONDAY:', NOT 'case Day.MONDAY:'.

[HI] पारंपरिक जावा switch byte, short, char, int, उनके रैपर प्रकार (Byte, Short, Character, Integer), enum (Java 5 से) और String (Java 7 से) का समर्थन करता है। इसके विपरीत, जावा long, float, double और boolean पर switch की अनुमति नहीं देता—इनमें से किसी का भी उपयोग करने पर तुरंत कंपाइल-टाइम त्रुटि आती है। प्रत्येक 'case' लेबल एक कंपाइल-टाइम स्थिरांक (लिटरल या final स्थिरांक) होना चाहिए और डुप्लीकेट case मान अनुमत नहीं हैं। enum पर switch करते समय case में केवल स्थिरांक का नाम (जैसे 'case MONDAY:', न कि 'case Day.MONDAY:') लिखना अनिवार्य होता है।

2. Fall-Through Execution, break, and default Placement (फॉल-थ्रू निष्पादन, break और default का स्थान)

[EN] Once a switch finds a matching case label (or jumps to 'default'), execution flows sequentially downward through every subsequent statement—even across other case labels—until hitting a 'break', 'return', 'throw', or the closing brace '}'. This phenomenon, known as fall-through, is intentionally exploited to group multiple cases under one action (such as case 'a': case 'e': case 'i': case 'o': case 'u':), but accidental omission of 'break' is a notorious source of bugs. Although convention places 'default:' at the bottom of the switch block, Java permits 'default:' anywhere—including as the very first label—and only jumps to it if no explicit case matches. However, if 'default:' sits above other cases without a break and gets triggered, execution will fall through into the cases below it!

[HI] जैसे ही switch किसी case लेबल से मेल खाता है (या 'default' पर जाता है), निष्पादन 'break', 'return', 'throw' या ब्लॉक अंत '}' मिलने तक नीचे की सभी पंक्तियों और अन्य case लेबल्स से होकर क्रमिक रूप से चलता रहता है। इस व्यवहार को 'फॉल-थ्रू' कहते हैं; इसका जानबूझकर उपयोग कई केसों को एक साथ समूहीकृत करने (जैसे स्वर वर्ण जांचना) के लिए होता है, पर break भूल जाने से बग आ सकता है। यद्यपि परंपरागत रूप से 'default:' को सबसे नीचे लिखा जाता है, जावा इसे switch के भीतर कहीं भी (शुरुआत में भी) रखने की अनुमति देता है, और कोई case न मिलने पर ही यह चलता है। यदि ऊपर लिखा गया default बिना break के चले, तो वह अपने नीचे के केसों में फॉल-थ्रू कर जाएगा।

3. String Switch Internals (Java 7+) and Null Safety (String switch की आंतरिक कार्यप्रणाली (Java 7+) और Null सुरक्षा)

[EN] Introduced in Java 7, switching on a String does NOT require JVM bytecode extensions; instead, javac compiles String switch into a two-step lookup using String.hashCode() followed by String.equals() to guard against hash collisions. String comparisons inside switch are strictly case-sensitive ("Java" does not match "java"). Because the compiler calls str.hashCode() internally when entering the switch, passing a 'null' String (or null enum/Integer wrapper) into a traditional switch immediately throws java.lang.NullPointerException! Therefore, always check 'if (str != null)' before entering a traditional switch (unless using Java 21+ pattern matching with explicit 'case null').

[HI] Java 7 में जोड़ा गया String switch JVM बाइटकोड में बदलाव किए बिना चलता है; javac इसे दो-चरणीय लुकअप में कंपाइल करता है जिसमें पहले String.hashCode() और फिर हैश टकराव रोकने हेतु String.equals() की जांच होती है। switch में स्ट्रिंग मिलान पूरी तरह केस-सेंसिटिव होता है ("Java" और "java" भिन्न माने जाते हैं)। चूंकि switch में प्रवेश करते ही आंतरिक रूप से hashCode() या अनबॉक्सिंग होती है, इसलिए पारंपरिक switch को 'null' String, null enum या null Integer देने पर तुरंत NullPointerException उत्पन्न होता है। अतः पारंपरिक switch से पहले null की जांच करना सुरक्षित अभ्यास है।

4. Modern Switch Expressions with Arrow (->) and yield (Java 14+) (आधुनिक Switch एक्सप्रेशन: एरो (->) और yield (Java 14+))

[EN] Java 14 standardized Switch Expressions (previewed in Java 12/13) to eliminate accidental fall-through bugs and allow switch to return values directly. Using the arrow syntax 'case 1, 2 -> "Low";', multiple comma-separated labels can share one action and NO 'break' is needed because arrow branches never fall through. When an arrow branch uses a multi-statement { block }, the 'yield value;' statement supplies the resulting value for the switch expression. Unlike switch statements, switch expressions returning a value must be exhaustive (typically requiring a 'default' branch or covering every enum constant).

[HI] Java 14 ने आकस्मिक फॉल-थ्रू त्रुटियों को समाप्त करने और switch से सीधे मान लौटाने के लिए Switch Expressions को मानकीकृत किया। एरो सिंटैक्स 'case 1, 2 -> "Low";' में अल्पविराम से कई मान एक साथ लिखे जा सकते हैं और 'break' की आवश्यकता नहीं रहती क्योंकि एरो शाखाएँ कभी फॉल-थ्रू नहीं करतीं। जब किसी एरो शाखा में एकाधिक कथनों वाला { ब्लॉक } हो, तो परिणाम लौटाने के लिए 'yield value;' का उपयोग किया जाता है। मान लौटाने वाले switch एक्सप्रेशन का पूर्ण (exhaustive) होना अनिवार्य है, इसलिए सामान्यतः इसमें default या सभी enum स्थिरांक होने चाहिए।

Syntax, Formula & Technical Example (switch Statement in Java: Primitive, String, Enum Support)

public class SwitchDemo {
    enum Role { ADMIN, USER, GUEST }
    public static void main(String[] args) {
        switch ("MON") { // String supported since Java 7
            case "MON": case "TUE": System.out.println("Weekday"); break;
            default: System.out.println("Other day"); }
        String p = switch (Role.ADMIN) { case ADMIN -> "FULL"; case USER, GUEST -> "LIMITED"; };
        System.out.println("Permission: " + p);
    }
}

switch Statement in Java: Primitive, String, Enum Support Quick Reference & Comparison Table

Switch Type / FeatureSupported DetailsKey Exam Fact
Integral Primitivesbyte, short, char, int (+ wrapper unboxing)long, float, double, and boolean are forbidden
String (Java 7+)Case-sensitive; compiled via hashCode() + equals()Throws NullPointerException if switched String is null
Enum (Java 5+)Type-safe named constantsUse unqualified label (case RED:, not case Color.RED:)
Arrow Switch (Java 14+)Uses -> and yield without fall-throughSwitch expressions must be exhaustive across all cases

switch Statement in Java: Primitive, String, Enum Support Top 20 Bilingual MCQs with Answers

Q1. Which primitive data type is NOT permitted inside a Java switch(...) expression?

(जावा के switch(...) व्यंजक के भीतर निम्नलिखित में से कौन सा प्रिमिटिव डेटा प्रकार अनुमत नहीं है?)

(A) short

(B) char

(C) long

(D) byte

Correct Answer: (C)

[EN] Java allows byte, short, char, and int in switch, while explicitly prohibiting long, float, double, and boolean.

[HI] जावा switch में byte, short, char और int की अनुमति देता है, जबकि long, float, double और boolean पूरी तरह निषिद्ध हैं।

Q2. In which Java version was support for 'String' in switch statements introduced?

(switch स्टेटमेंट में 'String' के लिए समर्थन जावा के किस संस्करण में जोड़ा गया था?)

(A) Java 7

(B) Java 8

(C) Java 11

(D) Java 5

Correct Answer: (A)

[EN] Java 5 introduced enum and wrapper unboxing for switch, whereas Java 7 added String support.

[HI] Java 5 ने enum और रैपर अनबॉक्सिंग जोड़ा था, जबकि String का समर्थन Java 7 (JDK 1.7) में आया।

Q3. What happens if you pass a null String reference into a traditional switch(str) statement?

(यदि आप पारंपरिक switch(str) स्टेटमेंट को एक null String रेफरेंस दें तो क्या होगा?)

(A) Compile-time error

(B) Throws NullPointerException at runtime

(C) Jumps automatically to the default branch

(D) Skips the entire switch silently

Correct Answer: (B)

[EN] The compiled bytecode invokes str.hashCode() on entry to switch(str), immediately throwing NullPointerException when str is null.

[HI] संकलित बाइटकोड switch(str) में प्रवेश करते ही str.hashCode() कॉल करता है, जिससे null होने पर तुरंत NullPointerException उत्पन्न होता है।

Q4. What is the output of: int x = 2; switch(x) { case 1: System.out.print("A"); case 2: System.out.print("B"); case 3: System.out.print("C"); default: System.out.print("D"); }

(int x = 2; switch(x) { case 1: System.out.print("A"); case 2: System.out.print("B"); case 3: System.out.print("C"); default: System.out.print("D"); } का आउटपुट क्या होगा?)

(A) B

(B) ABCD

(C) BCD

(D) BC

Correct Answer: (C)

[EN] Execution jumps to case 2, prints "B", and because there are no break statements, falls through into case 3 ("C") and default ("D").

[HI] निष्पादन सीधे case 2 पर जाता है और break न होने के कारण नीचे के case 3 व default में फॉल-थ्रू होकर "BCD" प्रिंट करता है।

Q5. What happens if 'default:' is placed as the FIRST label inside a switch block without a break: switch(5) { default: print("D"); case 1: print("1"); break; }

(यदि 'default:' को switch ब्लॉक में सबसे पहले बिना break के रखा जाए: switch(5) { default: print("D"); case 1: print("1"); break; } तो क्या प्रिंट होगा?)

(A) D

(B) Compile-time error because default must be last

(C) D1

(D) 1

Correct Answer: (C)

[EN] default can appear anywhere in a switch block; since 5 matches no case, execution enters default (prints D) and falls through into case 1 (prints 1) until break.

[HI] default ब्लॉक में कहीं भी हो सकता है; कोई केस मैच न होने पर वह default से शुरू होकर बिना break के case 1 में फॉल-थ्रू करते हुए "D1" प्रिंट करता है।

Q6. Which code causes a compile-time error inside a switch on variable 'int n'?

('int n' पर बने switch के भीतर निम्नलिखित में से कौन सा कोड कंपाइल-टाइम त्रुटि देगा?)

(A) byte b = 5; switch(b) { case 5: break; }

(B) int k = 10; switch(n) { case k: break; }

(C) switch(n) { default: break; }

(D) final int k = 10; switch(n) { case k: break; }

Correct Answer: (B)

[EN] case labels must be compile-time constant expressions. A non-final local variable 'int k = 10' is rejected by javac ('constant expression required').

[HI] case लेबल कंपाइल-टाइम स्थिरांक होना अनिवार्य है; नॉन-final वेरिएबल 'k' का उपयोग करने पर कंपाइल त्रुटि आती है।

Q7. When switching on 'enum Color { RED, GREEN, BLUE }', how must case labels be written?

('enum Color { RED, GREEN, BLUE }' पर switch लिखते समय case लेबल कैसे लिखे जाने चाहिए?)

(A) case Color.RED:

(B) case Color::RED:

(C) case "RED":

(D) case RED:

Correct Answer: (D)

[EN] Java mandates unqualified enum constant names in switch case labels; writing 'case Color.RED:' causes a compile-time error.

[HI] जावा में enum के case लेबल में केवल स्थिरांक नाम (case RED:) लिखा जाता है; 'case Color.RED:' लिखने पर कंपाइल त्रुटि आती है।

Q8. What occurs if two identical case values appear in the same switch block (e.g., case 'A': and case 65:)?

(यदि एक ही switch ब्लॉक में दो समान मान वाले case आ जाएँ (जैसे case 'A': और case 65:), तो क्या होगा?)

(A) The first matching case executes

(B) The second case overrides the first

(C) Compile-time error: duplicate case label

(D) Runtime IllegalStateException

Correct Answer: (C)

[EN] In Unicode/ASCII, 'A' equals 65, so case 'A' and case 65 represent the exact same constant value, triggering a duplicate case compile error.

[HI] यूनिकोड में 'A' का संख्यात्मक मान 65 होता है, इसलिए case 'A' और case 65 एक ही मान होने से डुप्लीकेट केस की कंपाइल त्रुटि आती है।

Q9. What happens when you write: byte b = 10; switch(b) { case 10: break; case 200: break; }

(byte b = 10; switch(b) { case 10: break; case 200: break; } लिखने पर क्या होगा?)

(A) Automatically promotes b to int

(B) Throws ArithmeticException at runtime

(C) Compile-time error because 200 exceeds the byte range (-128 to 127)

(D) Compiles cleanly and ignores case 200

Correct Answer: (C)

[EN] Every case constant must be assignable to the switch expression's type; 200 does not fit inside byte (-128..127).

[HI] प्रत्येक case स्थिरांक switch वेरिएबल के प्रकार की सीमा में होना आवश्यक है; 200 byte (-128..127) से बाहर है, अतः कंपाइल त्रुटि आएगी।

Q10. How does javac implement 'switch(String)' under the hood?

(javac आंतरिक रूप से 'switch(String)' को कैसे लागू करता है?)

(A) It calls String.compareTo() in a binary search

(B) It compares memory references using ==

(C) It generates bytecode switching on hashCode() first and then verifies exact matches with equals()

(D) It converts the String into a single char sum

Correct Answer: (C)

[EN] Two different Strings can share the same hashCode() (hash collision), so the compiler pairs hashCode() switching with .equals() checks for correctness.

[HI] अलग-अलग Strings का hashCode() समान हो सकता है, इसलिए कंपाइलर पहले hashCode() से मिलान करके फिर .equals() द्वारा सटीक जांच करता है।

Q11. Which keyword was introduced in Java 14 to return a value from a multi-statement block inside a switch expression?

(Java 14 में switch एक्सप्रेशन के बहु-कथन ब्लॉक से मान लौटाने के लिए कौन सा कीवर्ड पेश किया गया?)

(A) break

(B) return

(C) yield

(D) emit

Correct Answer: (C)

[EN] Inside arrow switch expression blocks (case X -> { ... yield val; }), 'yield' specifies the value produced by the switch expression.

[HI] एरो switch एक्सप्रेशन ब्लॉक के भीतर परिणाम मान देने के लिए 'yield' कीवर्ड का उपयोग किया जाता है।

Q12. In Java 14+ arrow switch syntax (case 1 -> System.out.println("One");), is a 'break' statement needed to avoid fall-through?

(Java 14+ के एरो switch सिंटैक्स (case 1 -> ...) में फॉल-थ्रू रोकने के लिए क्या 'break' की आवश्यकता होती है?)

(A) Only when switching on Strings

(B) No, arrow (->) switch branches never fall through

(C) Yes, unless yield is used

(D) Yes, break is still required after every arrow case

Correct Answer: (B)

[EN] Arrow-form switch rules execute only the single matched statement, block, or throw and automatically exit the switch without fall-through.

[HI] एरो (->) सिंटैक्स में कभी फॉल-थ्रू नहीं होता; केवल मिलान हुई शाखा चलती है और switch अपने आप समाप्त हो जाता है।

Q13. What happens if you declare 'final int x; x = 5;' (blank final) and use 'case x:' in switch?

(यदि आप 'final int x; x = 5;' (ब्लैंक final) घोषित करके switch में 'case x:' लिखें तो क्या होगा?)

(A) Compiles cleanly because x is final

(B) Compiles with a deprecation warning

(C) Works only inside static methods

(D) Compile-time error because a blank final initialized on a separate statement is not a compile-time constant

Correct Answer: (D)

[EN] Per JLS §15.28, a final variable qualifies as a constant expression only if initialized with a constant expression directly in its declaration.

[HI] JLS के अनुसार, कोई final वेरिएबल केवल तभी कंपाइल-टाइम स्थिरांक माना जाता है जब उसे घोषणा के समय ही मान दिया गया हो।

Q14. What is the output of: int ch = 'b'; switch(ch) { case 'a': print(1); break; case 'b': print(2); case 'c': print(3); break; }

(int ch = 'b'; switch(ch) { case 'a': print(1); break; case 'b': print(2); case 'c': print(3); break; } का आउटपुट क्या होगा?)

(A) 23

(B) 2

(C) 123

(D) Compile-time error assigning char to int

Correct Answer: (A)

[EN] 'b' widens cleanly to int (98), matches case 'b', prints 2, falls through into case 'c', prints 3, and exits at break.

[HI] 'b' का int मान 98 case 'b' से मेल खाता है, जिससे 2 प्रिंट होता है और break न होने से case 'c' में फॉल-थ्रू होकर 3 भी प्रिंट होता है (23)।

Q15. Can an empty switch statement 'switch(x) {}' compile in Java?

(क्या जावा में खाली switch स्टेटमेंट 'switch(x) {}' कंपाइल हो सकता है?)

(A) No, at least one case is mandatory

(B) No, default is strictly mandatory in every switch

(C) Yes, a switch statement with zero cases and no default is syntactically valid

(D) Compiles only when x is an enum

Correct Answer: (C)

[EN] A traditional switch statement (not an expression returning a value) compiles validly even with an empty body {}, evaluating x and doing nothing.

[HI] पारंपरिक switch स्टेटमेंट के अंदर शून्य case और बिना default के खाली बॉडी {} सिंटैक्स की दृष्टि से वैध है और कंपाइल हो जाती है।

Q16. Which statement about Java's switch construct is FALSE?

(जावा के switch निर्माण के बारे में निम्नलिखित में से कौन सा कथन असत्य है?)

(A) A break statement exits only the innermost enclosing switch or loop

(B) String matching inside switch is strictly case-sensitive

(C) Switch case labels can evaluate runtime ranges such as 'case x > 10:' in standard Java

(D) Switch supports Byte, Short, Character, and Integer wrapper types via unboxing

Correct Answer: (C)

[EN] Standard Java switch cases test discrete constant values (or Java 21+ type patterns with 'when' guards), never arbitrary relational expressions like 'case x > 10:' directly.

[HI] मानक जावा switch के case लेबल सीधे 'case x > 10:' जैसी असमानता शर्तों को नहीं जांच सकते; वे निश्चित स्थिरांकों से मिलान करते हैं।

Q17. Within switch Statement in Java: Primitive, String, Enum Support, which switch type / feature directly maps to: "byte, short, char, int (+ wrapper unboxing)"?

(switch Statement in Java: Primitive, String, Enum Support में कौन सा Switch Type / Feature सीधे "byte, short, char, int (+ wrapper unboxing)" से संबंधित है?)

(A) Arrow Switch (Java 14+)

(B) Enum (Java 5+)

(C) Integral Primitives

(D) String (Java 7+)

Correct Answer: (C)

[EN] In switch Statement in Java: Primitive, String, Enum Support, 'Integral Primitives' handles 'byte, short, char, int (+ wrapper unboxing)' (long, float, double, and boolean are forbidden).

[HI] switch Statement in Java: Primitive, String, Enum Support में 'Integral Primitives' का कार्य/विवरण 'byte, short, char, int (+ wrapper unboxing)' (long, float, double, and boolean are forbidden) है।

Q18. Which option accurately describes 'String (Java 7+)' in switch Statement in Java: Primitive, String, Enum Support?

(switch Statement in Java: Primitive, String, Enum Support के संदर्भ में 'String (Java 7+)' की सही विशेषता कौन सी है?)

(A) Type-safe named constants

(B) Case-sensitive; compiled via hashCode() + equals()

(C) Uses -> and yield without fall-through

(D) byte, short, char, int (+ wrapper unboxing)

Correct Answer: (B)

[EN] 'String (Java 7+)' provides 'Case-sensitive; compiled via hashCode() + equals()' — key point: Throws NullPointerException if switched String is null.

[HI] 'String (Java 7+)' का मुख्य विवरण 'Case-sensitive; compiled via hashCode() + equals()' है (Throws NullPointerException if switched String is null)।

Q19. In switch Statement in Java: Primitive, String, Enum Support, identify the correct parameter for the specification: "Type-safe named constants".

(switch Statement in Java: Primitive, String, Enum Support में "Type-safe named constants" विवरण किस विकल्प के लिए सही है?)

(A) String (Java 7+)

(B) Integral Primitives

(C) Enum (Java 5+)

(D) Arrow Switch (Java 14+)

Correct Answer: (C)

[EN] 'Enum (Java 5+)' corresponds to 'Type-safe named constants' (Use unqualified label (case RED:, not case Color.RED:)).

[HI] 'Enum (Java 5+)' का संबंध 'Type-safe named constants' (Use unqualified label (case RED:, not case Color.RED:)) से है।

Q20. Considering switch Statement in Java: Primitive, String, Enum Support, what key behavior defines 'Arrow Switch (Java 14+)'?

(switch Statement in Java: Primitive, String, Enum Support में 'Arrow Switch (Java 14+)' की मुख्य भूमिका या मान क्या है?)

(A) Case-sensitive; compiled via hashCode() + equals()

(B) Type-safe named constants

(C) Uses -> and yield without fall-through

(D) byte, short, char, int (+ wrapper unboxing)

Correct Answer: (C)

[EN] 'Arrow Switch (Java 14+)' represents 'Uses -> and yield without fall-through' (Switch expressions must be exhaustive across all cases).

[HI] 'Arrow Switch (Java 14+)' का अर्थ/मान 'Uses -> and yield without fall-through' है (Switch expressions must be exhaustive across all cases)।

switch Statement in Java: Primitive, String, Enum Support — Exam FAQs

Q1: Why doesn't Java allow float or double in a switch statement?

जावा switch स्टेटमेंट में float या double की अनुमति क्यों नहीं देता?

[EN] Floating-point numbers suffer from binary representation rounding inaccuracies (e.g., 0.1 + 0.2 != 0.3), making exact bit-level equality matching unreliable for jump-table or lookup-table dispatch.

[HI] फ़्लोटिंग-पॉइंट संख्याओं में बाइनरी राउंडिंग त्रुटियाँ होती हैं (जैसे 0.1 + 0.2 != 0.3), जिससे switch जैसे सटीक मान-मिलान और जंप-टेबल के लिए वे अविश्वसनीय होते हैं।

Q2: Is switch faster than an if-else-if ladder in Java?

क्या जावा में switch, if-else-if लैडर से तेज़ चलता है?

[EN] Often yes for dense or many constants: javac compiles dense integer cases into a 'tableswitch' bytecode instruction (O(1) direct jump table) and sparse cases into 'lookupswitch' (O(log n) binary search), whereas if-else-if evaluates O(n) sequentially.

[HI] कई निश्चित पूर्णांक विकल्पों के लिए हाँ: javac सघन केसों को O(1) वाले 'tableswitch' जंप-टेबल में और विरल केसों को O(log n) वाले 'lookupswitch' बाइनरी सर्च में बदलता है, जबकि if-else-if क्रमशः जांच करता है।

Q3: How does 'break' behave when a switch statement is placed inside a for or while loop?

जब कोई switch स्टेटमेंट for या while लूप के भीतर हो, तो 'break' कैसे काम करता है?

[EN] An unlabeled 'break;' inside a switch exits only the switch block and does NOT exit the enclosing loop. To break out of the outer loop from inside a switch, use a labeled break such as 'break outerLoop;'.

[HI] switch के भीतर बिना लेबल वाला 'break;' केवल switch ब्लॉक से बाहर निकलता है, बाहरी लूप को नहीं रोकता। बाहरी लूप से बाहर निकलने के लिए लेबल वाले break (break outer;) की आवश्यकता होती है।

Related Posts

  • Loading related posts...

Post a Comment

Search Topic Notes & 20 MCQs (Hindi / English)