PGDCA, BCA, B-Tech, MCA, M-Tech, B.Sc computer science राजस्थान सूचना सहायक व कंप्यूटर शिक्षक विशेष नोट्स: इस अध्याय में हम Type Casting in Java: Widening (Implicit) vs Narrowing (Explicit) (टाइप कास्टिंग: वाइडनिंग (ऑटोमैटिक) बनाम नैरोइंग (मैनुअल)) की संपूर्ण थ्योरी, मुख्य तकनीकी बिंदु, तुलनात्मक तालिका और विगत परीक्षाओं के पैटर्न पर आधारित 20 अति-महत्वपूर्ण बहुविकल्पीय प्रश्न (MCQs) विस्तृत हिंदी व अंग्रेजी व्याख्या सहित पढ़ेंगे।
Overview: Type casting in Java converts a value or object reference from one compatible data type to another, categorized into automatic Widening (Implicit) and manual Narrowing (Explicit) conversions. Understanding numeric promotion rules, precision-loss boundaries, and runtime ClassCastException guards is essential for writing bug-free Java expressions and passing competitive exams.
अध्याय सार: जावा में टाइप कास्टिंग किसी मान या ऑब्जेक्ट रेफरेंस को एक संगत डेटा प्रकार से दूसरे प्रकार में परिवर्तित करती है, जिसे स्वचालित वाइडनिंग (Implicit) और मैनुअल नैरोइंग (Explicit) में बाँटा जाता है। त्रुटि-रहित जावा कोड और परीक्षाओं के लिए संख्यात्मक प्रमोशन नियमों, सटीकता हानि की सीमाओं और ClassCastException से बचाव को समझना अत्यंत आवश्यक है।
Type Casting in Java: Widening (Implicit) vs Narrowing (Explicit): Detailed Notes & Architecture (टाइप कास्टिंग: वाइडनिंग (ऑटोमैटिक) बनाम नैरोइंग (मैनुअल))
1. Widening (Implicit) Primitive Conversion and Precision Edge Cases (वाइडनिंग (इम्प्लिसिट) प्रिमिटिव रूपांतरण और सटीकता सीमाएँ)
[EN] Widening conversion automatically promotes a smaller primitive type into a larger compatible type without an explicit cast operator along the path: byte -> short -> int -> long -> float -> double, and char -> int -> long -> float -> double. Because the destination range encompasses the source range, javac performs widening silently during assignments and method calls. A classic JLS subtlety is that widening preserves overall magnitude rather than always preserving precision: converting int to float (24-bit significand) or long to float/double (53-bit significand) can lose the least significant digits automatically without compiler warning. Notice also that byte and short cannot widen automatically into char (and char cannot widen into short) because byte/short are signed while char is unsigned.
[HI] वाइडनिंग रूपांतरण बिना किसी कास्ट ऑपरेटर के छोटे प्रिमिटिव प्रकार को स्वतः बड़े संगत प्रकार में बदल देता है: byte -> short -> int -> long -> float -> double, तथा char -> int -> long -> float -> double। चूंकि गंतव्य रेंज स्रोत से बड़ी होती है, इसलिए javac असाइनमेंट और मेथड कॉल में यह रूपांतरण स्वतः कर देता है। एक महत्वपूर्ण बारीकी यह है कि int से float या long से float/double में स्वतः वाइडनिंग के दौरान परिमाण (magnitude) तो बना रहता है लेकिन कम महत्वपूर्ण अंकों की सटीकता (precision) घट सकती है। ध्यान रखें कि byte और short स्वतः char में नहीं बदल सकते क्योंकि byte/short साइन्ड हैं जबकि char अनसाइन्ड है।
2. Narrowing (Explicit) Primitive Casting and Data Loss (नैरोइंग (एक्सप्लिसिट) प्रिमिटिव कास्टिंग और डेटा हानि)
[EN] Converting from a larger or floating-point primitive into a smaller type (double -> float -> long -> int -> short -> byte, or between char and byte/short) requires an explicit cast operator such as '(int) value'. When narrowing a floating-point number to an integer (e.g., (int) 19.99), Java truncates the decimal fraction toward zero, producing 19 rather than rounding. When narrowing a larger integer to a smaller integer (e.g., (byte) 130), Java discards upper bits and retains only the lowest target bits, causing two's-complement wrap-around ((byte) 130 == -126). Implicit narrowing happens in only two places: assigning a compile-time constant expression that fits within byte/short/char, or using compound assignment operators like b += 5.
[HI] बड़े या दशमलव प्रकार से छोटे प्रिमिटिव प्रकार (double -> float -> long -> int -> short -> byte, या char व byte/short के बीच) में बदलने के लिए '(int) value' जैसा स्पष्ट कास्ट ऑपरेटर आवश्यक होता है। फ़्लोटिंग-पॉइंट को पूर्णांक में नैरो करते समय (जैसे (int) 19.99) जावा दशमलव भाग को राउंड करने के बजाय शून्य की ओर काट (truncate) देता है, जिससे 19 मिलता है। बड़े पूर्णांक को छोटे पूर्णांक में नैरो करने पर (जैसे (byte) 130) ऊपरी बिट्स कट जाते हैं और केवल निचले बिट्स शेष रहते हैं, जिससे टूज़-कॉम्प्लिमेंट रैप-अराउंड (-126) होता है। स्वचालित नैरोइंग केवल तभी होता है जब दाईं ओर सीमा में आने वाला कंपाइल-टाइम स्थिरांक हो या += जैसे कंपाउंड असाइनमेंट ऑपरेटर का उपयोग हो।
3. Binary Numeric Promotion in Expressions (व्यंजकों (Expressions) में बाइनरी न्यूमेरिक प्रमोशन)
[EN] Whenever an arithmetic, bitwise, or shift operator evaluates operands smaller than int—specifically byte, short, or char—Java automatically promotes those operands to 'int' before computing the result. Consequently, writing 'byte a = 10, b = 20; byte c = a + b;' fails to compile with 'possible lossy conversion from int to byte' unless written as 'byte c = (byte) (a + b);'. For mixed-type binary expressions, Java promotes both operands to the widest participating type in the hierarchy: double > float > long > int. For example, evaluating 'int + long * double' promotes every operand to double and produces a double result.
[HI] जब भी कोई अंकगणितीय, बिटवाइज़ या शिफ्ट ऑपरेटर int से छोटे ऑपरेंड—विशेषकर byte, short या char—पर कार्य करता है, तो जावा गणना से पहले उन्हें स्वतः 'int' में प्रमोट कर देता है। इसी कारण 'byte a = 10, b = 20; byte c = a + b;' लिखने पर कंपाइल-टाइम त्रुटि आती है और इसे 'byte c = (byte) (a + b);' लिखना पड़ता है। मिश्रित प्रकार वाले बाइनरी व्यंजकों में जावा दोनों ऑपरेंड को double > float > long > int क्रम के अनुसार सबसे बड़े प्रकार में प्रमोट करता है। उदाहरणार्थ, 'int + long * double' का अंतिम परिणाम प्रकार हमेशा double होता है।
4. Reference Casting: Upcasting, Downcasting, and instanceof (रेफरेंस कास्टिंग: अपकास्टिंग, डाउनकास्टिंग और instanceof)
[EN] For non-primitive objects, casting changes the reference view without altering the underlying Heap object. Upcasting (e.g., Animal a = new Dog();) widens a subclass reference to a superclass or interface type; it is always implicit and guaranteed safe at compile time and runtime. Downcasting (e.g., Dog d = (Dog) a;) explicitly narrows a supertype reference back to a subtype to access subclass-specific methods; if 'a' actually points to a Cat at runtime, the JVM throws ClassCastException. Safe Java code guards downcasts using 'if (a instanceof Dog)' (or Java 16+ pattern matching 'if (a instanceof Dog d)') prior to invoking Dog methods.
[HI] नॉन-प्रिमिटिव ऑब्जेक्ट्स में कास्टिंग मूल हीप ऑब्जेक्ट को बदले बिना केवल रेफरेंस के दृष्टिकोण को बदलती है। अपकास्टिंग (जैसे Animal a = new Dog();) सबक्लास रेफरेंस को सुपरक्लास या इंटरफ़ेस में बदलती है; यह स्वतः होती है और हमेशा सुरक्षित रहती है। डाउनकास्टिंग (जैसे Dog d = (Dog) a;) सुपरटाइप रेफरेंस को स्पष्ट रूप से सबटाइप में बदलती है; यदि रनटाइम पर 'a' वास्तव में Cat ऑब्जेक्ट को इंगित कर रहा हो, तो JVM ClassCastException फेंकता है। सुरक्षित जावा कोड डाउनकास्ट करने से पहले 'if (a instanceof Dog)' (या Java 16+ पैटर्न मैचिंग 'if (a instanceof Dog d)') से जांच करता है।
Syntax, Formula & Technical Example (Type Casting in Java: Widening (Implicit) vs Narrowing (Explicit))
public class TypeCastingDemo {
public static void main(String[] args) {
int count = 100; double wide = count; // Widening (implicit): 100.0
double price = 99.95; int truncated = (int) price; // Narrowing: 99
byte wrapped = (byte) 130; // Overflow wrap: -126
byte x = 10, y = 20;
byte sum = (byte) (x + y); // Binary promotion to int requires cast
System.out.println(wide + ", " + truncated + ", " + wrapped + ", " + sum);
}
}
Type Casting in Java: Widening (Implicit) vs Narrowing (Explicit) Quick Reference & Comparison Table
| Casting Mechanism | Direction & Syntax | Key Exam Fact |
|---|---|---|
| Widening (Implicit) | Smaller to larger primitive (no cast operator) | Automatic; int->float and long->double may still lose precision |
| Narrowing (Explicit) | Larger to smaller primitive using (type) | Truncates decimals toward zero; overflows wrap modulo 2^n |
| Expression Promotion | byte/short/char promote automatically to int | byte c = a + b fails to compile without (byte)(a + b) |
| Reference Downcasting | Supertype reference to subtype via (SubClass) | Throws ClassCastException if heap object mismatches; guard with instanceof |
Type Casting in Java: Widening (Implicit) vs Narrowing (Explicit) Top 20 Bilingual MCQs with Answers
Q1. What is the exact output of: double d = 19.99; int i = (int) d; System.out.println(i);
(double d = 19.99; int i = (int) d; System.out.println(i); का सटीक आउटपुट क्या होगा?)
(A) 19.0
(B) Compile-time error
(C) 19
(D) 20
Correct Answer: (C)
[EN] Explicit narrowing from double to int truncates the fractional part toward zero instead of rounding, yielding 19.
[HI] double से int में स्पष्ट नैरोइंग दशमलव भाग को राउंड करने के बजाय शून्य की ओर काट (truncate) देती है, जिससे परिणाम 19 आता है।
Q2. What value is stored in 'b' after executing: byte b = (byte) 130;
(byte b = (byte) 130; निष्पादित करने के बाद 'b' में कौन सा मान संग्रहीत होगा?)
(A) -128
(B) 130
(C) -126
(D) 127
Correct Answer: (C)
[EN] 130 in binary is 10000010 in 8 bits; interpreted as signed two's-complement byte, 130 - 256 = -126.
[HI] 8 बिट्स में 130 का बाइनरी 10000010 होता है; साइन्ड टूज़-कॉम्प्लिमेंट byte के रूप में इसका मान 130 - 256 = -126 होगा।
Q3. Why does 'byte a = 5, b = 6; byte c = a + b;' produce a compile-time error in Java?
(जावा में 'byte a = 5, b = 6; byte c = a + b;' कंपाइल-टाइम त्रुटि क्यों देता है?)
(A) 5 + 6 overflows the byte range
(B) The '+' operator promotes byte operands to int, and an int cannot implicitly narrow into byte
(C) Java requires using += instead of + for byte addition
(D) The '+' operator is undefined for byte variables
Correct Answer: (B)
[EN] Under Java's binary numeric promotion rules, arithmetic on byte/short/char always produces an int result.
[HI] जावा के बाइनरी न्यूमेरिक प्रमोशन नियमों के अनुसार, byte/short/char पर अंकगणित हमेशा int परिणाम देता है।
Q4. Which of the following primitive conversions is NOT performed implicitly (automatically) by Java?
(निम्नलिखित में से कौन सा प्रिमिटिव रूपांतरण जावा द्वारा स्वतः (implicitly) नहीं किया जाता है?)
(A) long to float
(B) short to char
(C) char to int
(D) int to long
Correct Answer: (B)
[EN] Because short is signed (-32,768 to 32,767) and char is unsigned (0 to 65,535), neither is a subset of the other, so converting between them requires an explicit cast.
[HI] चूंकि short साइन्ड (-32,768 से 32,767) है और char अनसाइन्ड (0 से 65,535) है, इसलिए दोनों एक-दूसरे के उपसमुच्चय नहीं हैं और इनके बीच स्पष्ट कास्ट अनिवार्य है।
Q5. Why does 'byte b = 10; b += 5;' compile cleanly even though 'b = b + 5;' fails to compile?
('b = b + 5;' के विफल होने के बावजूद 'byte b = 10; b += 5;' बिना किसी त्रुटि के कंपाइल क्यों हो जाता है?)
(A) javac disables type checking for compound operators
(B) b += 5 skips binary promotion to int
(C) The literal 5 is treated as a byte only inside +=
(D) Compound assignment operators (+=) automatically insert an implicit narrowing cast
Correct Answer: (D)
[EN] Per JLS §15.26.2, E1 += E2 evaluates as E1 = (T)((E1) + (E2)) where T is the type of E1, implicitly casting back to byte.
[HI] JLS नियमों के अनुसार कंपाउंड असाइनमेंट (E1 += E2) स्वतः ही E1 के प्रकार में इम्प्लिसिट नैरोइंग कास्ट जोड़ देता है।
Q6. Which widening conversion is automatic in Java yet may still lose precision for large values?
(जावा में कौन सा वाइडनिंग रूपांतरण स्वतः होता है लेकिन फिर भी बड़े मानों के लिए सटीकता (precision) खो सकता है?)
(A) short to int
(B) long to double
(C) int to long
(D) byte to short
Correct Answer: (B)
[EN] A 64-bit long has 63 value bits, whereas a 64-bit IEEE 754 double has only 53 bits of significand precision, so large longs round during automatic widening to double or float.
[HI] 64-बिट long में 63 मान बिट्स होते हैं, जबकि 64-बिट double में केवल 53 सटीकता बिट्स होते हैं; अतः बड़े long मानों के वाइडनिंग में सटीकता कम हो सकती है।
Q7. What happens when you write: Object obj = "Hello"; Integer num = (Integer) obj;
(Object obj = "Hello"; Integer num = (Integer) obj; लिखने पर क्या होता है?)
(A) Compile-time incompatible types error
(B) num holds the ASCII value of "Hello"
(C) num becomes null
(D) Compiles successfully, then throws ClassCastException at runtime
Correct Answer: (D)
[EN] The compiler allows downcasting from Object to Integer because Integer is a subclass of Object; at runtime, the JVM detects the heap object is String and throws ClassCastException.
[HI] चूंकि Integer, Object की सबक्लास है, इसलिए कोड कंपाइल हो जाता है; लेकिन रनटाइम पर हीप ऑब्जेक्ट String निकलने से JVM ClassCastException फेंकता है।
Q8. Which operator safely checks an object's actual runtime type before performing a downcast?
(डाउनकास्ट करने से पहले किसी ऑब्जेक्ट के वास्तविक रनटाइम प्रकार की सुरक्षित जांच कौन सा ऑपरेटर करता है?)
(A) isType
(B) castof
(C) typeof
(D) instanceof
Correct Answer: (D)
[EN] The binary operator 'obj instanceof ClassName' evaluates to true only if obj is non-null and is an instance of ClassName or its subclass/implementer.
[HI] 'obj instanceof ClassName' ऑपरेटर केवल तभी true देता है जब obj नॉन-null हो और वह ClassName या उसके सबटाइप का इंस्टेंस हो।
Q9. What is the resulting data type of the expression: (byte)1 + (short)2 + 3L + 4.0f?
(व्यंजक (byte)1 + (short)2 + 3L + 4.0f का परिणाम किस डेटा प्रकार का होगा?)
(A) int
(B) long
(C) float
(D) double
Correct Answer: (C)
[EN] byte and short promote to int; adding long (3L) promotes to long; adding float (4.0f) widens the entire expression to float.
[HI] byte और short पहले int बनते हैं; 3L जुड़ने पर परिणाम long होता है; और अंत में 4.0f जुड़ने से पूरा व्यंजक float में प्रमोट हो जाता है।
Q10. Can a 'boolean' value ever be cast to an 'int' using '(int) true' in Java?
(क्या जावा में किसी 'boolean' मान को '(int) true' लिखकर 'int' में कास्ट किया जा सकता है?)
(A) Yes, (int) true evaluates to 1
(B) Yes, (int) true compiles with a warning
(C) No, boolean cannot be cast to or from any other primitive type
(D) Yes, only when wrapped in Boolean
Correct Answer: (C)
[EN] Java forbids casting between boolean and all 7 numeric primitive types; doing so triggers a compile-time error.
[HI] जावा में boolean और अन्य 7 संख्यात्मक प्रिमिटिव प्रकारों के बीच किसी भी प्रकार की कास्टिंग पूरी तरह वर्जित है।
Q11. What does 'char ch = (char) -1; System.out.println((int) ch);' output?
('char ch = (char) -1; System.out.println((int) ch);' का आउटपुट क्या होगा?)
(A) 65536
(B) -1
(C) 0
(D) 65535
Correct Answer: (D)
[EN] Casting -1 (all 1-bits in two's complement) to 16-bit unsigned char yields 0xFFFF = 65535, which widens to int 65535.
[HI] -1 (सभी बिट्स 1) को 16-बिट अनसाइन्ड char में कास्ट करने पर 0xFFFF = 65535 मिलता है, जो int में वाइड होकर 65535 प्रिंट होता है।
Q12. Which of the following code snippets compiles WITHOUT an explicit cast?
(निम्नलिखित में से कौन सा कोड स्निपेट बिना किसी स्पष्ट कास्ट के कंपाइल हो जाएगा?)
(A) byte x = 10; byte y = 20; byte z = x + y;
(B) final byte x = 10; final byte y = 20; byte z = x + y;
(C) double d = 5.0; float f = d;
(D) int i = 65; char c = i;
Correct Answer: (B)
[EN] Because x and y are 'final' compile-time constants whose sum (30) fits within byte range (-128..127), javac performs implicit narrowing.
[HI] चूंकि x और y 'final' कंपाइल-टाइम स्थिरांक हैं और उनका योग (30) byte की रेंज में है, इसलिए javac स्वतः इम्प्लिसिट नैरोइंग कर देता है।
Q13. What is 'Upcasting' in Java object-oriented programming?
(जावा ऑब्जेक्ट-ओरिएंटेड प्रोग्रामिंग में 'Upcasting' क्या है?)
(A) Assigning a subclass object reference to a superclass or interface variable
(B) Promoting a byte variable into a long variable inside a loop
(C) Converting a double variable into an int variable
(D) Converting a superclass reference forcibly into a subclass reference
Correct Answer: (A)
[EN] Upcasting moves up the inheritance hierarchy (e.g., List list = new ArrayList();) implicitly to achieve runtime polymorphism.
[HI] अपकास्टिंग इनहेरिटेंस पदानुक्रम में ऊपर की ओर रेफरेंस असाइन करना है (जैसे List list = new ArrayList();), जो पॉलीमॉर्फ़िज़्म के लिए स्वतः होता है।
Q14. Which compile-time error occurs if you write: String s = (String) new StringBuilder("hi");
(String s = (String) new StringBuilder("hi"); लिखने पर क्या होगा?)
(A) Returns null at runtime
(B) Compile-time error: incompatible types (unrelated classes cannot be cast)
(C) Compiles cleanly and throws ClassCastException at runtime
(D) Converts StringBuilder seamlessly into String
Correct Answer: (B)
[EN] The compiler rejects casts between two sibling/unrelated final classes that have no inheritance relationship in the hierarchy.
[HI] दो असंबद्ध क्लासेस जिनके बीच कोई इनहेरिटेंस संबंध नहीं है, उनके बीच कास्टिंग को कंपाइलर ही अस्वीकार कर देता है।
Q15. How do you correctly parse a numeric String "42" into a primitive int (since casting (int)"42" is invalid)?
(स्ट्रिंग "42" को प्रिमिटिव int में सही तरीके से कैसे बदला जाता है (चूंकि (int)"42" अमान्य है)?)
(A) Integer.parseInt("42")
(B) String.toInt("42")
(C) int.valueOf("42")
(D) (int) "42"
Correct Answer: (A)
[EN] Primitive casting never converts Strings; wrapper methods such as Integer.parseInt() or Double.parseDouble() parse text into numbers.
[HI] प्रिमिटिव कास्टिंग कभी String को संख्या में नहीं बदल सकती; इसके लिए Integer.parseInt() जैसे रैपर मेथड उपयोग किए जाते हैं।
Q16. Which statement about Type Casting in Java is FALSE?
(जावा में टाइप कास्टिंग के बारे में निम्नलिखित में से कौन सा कथन असत्य है?)
(A) Reference downcasting alters the actual object type stored inside the JVM Heap
(B) Explicit narrowing of floating-point to integer truncates fractional digits
(C) Widening primitive conversions occur automatically along byte->short->int->long->float->double
(D) Unary plus (+b) on a byte promotes its result expression to int
Correct Answer: (A)
[EN] Casting a reference never changes the object inside the Heap; it only changes the compile-time type of the reference pointer (and triggers ClassCastException if incompatible).
[HI] रेफरेंस कास्टिंग कभी भी हीप में मौजूद वास्तविक ऑब्जेक्ट के प्रकार को नहीं बदलती; यह केवल रेफरेंस वेरिएबल का व्यू प्रकार बदलती है।
Q17. Within Type Casting in Java: Widening (Implicit) vs Narrowing (Explicit), which casting mechanism directly maps to: "Smaller to larger primitive (no cast operator)"?
(Type Casting in Java: Widening (Implicit) vs Narrowing (Explicit) में कौन सा Casting Mechanism सीधे "Smaller to larger primitive (no cast operator)" से संबंधित है?)
(A) Widening (Implicit)
(B) Narrowing (Explicit)
(C) Expression Promotion
(D) Reference Downcasting
Correct Answer: (A)
[EN] In Type Casting in Java: Widening (Implicit) vs Narrowing (Explicit), 'Widening (Implicit)' handles 'Smaller to larger primitive (no cast operator)' (Automatic; int->float and long->double may still lose precision).
[HI] Type Casting in Java: Widening (Implicit) vs Narrowing (Explicit) में 'Widening (Implicit)' का कार्य/विवरण 'Smaller to larger primitive (no cast operator)' (Automatic; int->float and long->double may still lose precision) है।
Q18. Which option accurately describes 'Narrowing (Explicit)' in Type Casting in Java: Widening (Implicit) vs Narrowing (Explicit)?
(Type Casting in Java: Widening (Implicit) vs Narrowing (Explicit) के संदर्भ में 'Narrowing (Explicit)' की सही विशेषता कौन सी है?)
(A) Smaller to larger primitive (no cast operator)
(B) Larger to smaller primitive using (type)
(C) Supertype reference to subtype via (SubClass)
(D) byte/short/char promote automatically to int
Correct Answer: (B)
[EN] 'Narrowing (Explicit)' provides 'Larger to smaller primitive using (type)' — key point: Truncates decimals toward zero; overflows wrap modulo 2^n.
[HI] 'Narrowing (Explicit)' का मुख्य विवरण 'Larger to smaller primitive using (type)' है (Truncates decimals toward zero; overflows wrap modulo 2^n)।
Q19. In Type Casting in Java: Widening (Implicit) vs Narrowing (Explicit), identify the correct parameter for the specification: "byte/short/char promote automatically to int".
(Type Casting in Java: Widening (Implicit) vs Narrowing (Explicit) में "byte/short/char promote automatically to int" विवरण किस विकल्प के लिए सही है?)
(A) Expression Promotion
(B) Reference Downcasting
(C) Narrowing (Explicit)
(D) Widening (Implicit)
Correct Answer: (A)
[EN] 'Expression Promotion' corresponds to 'byte/short/char promote automatically to int' (byte c = a + b fails to compile without (byte)(a + b)).
[HI] 'Expression Promotion' का संबंध 'byte/short/char promote automatically to int' (byte c = a + b fails to compile without (byte)(a + b)) से है।
Q20. Considering Type Casting in Java: Widening (Implicit) vs Narrowing (Explicit), what key behavior defines 'Reference Downcasting'?
(Type Casting in Java: Widening (Implicit) vs Narrowing (Explicit) में 'Reference Downcasting' की मुख्य भूमिका या मान क्या है?)
(A) Smaller to larger primitive (no cast operator)
(B) byte/short/char promote automatically to int
(C) Larger to smaller primitive using (type)
(D) Supertype reference to subtype via (SubClass)
Correct Answer: (D)
[EN] 'Reference Downcasting' represents 'Supertype reference to subtype via (SubClass)' (Throws ClassCastException if heap object mismatches; guard with instanceof).
[HI] 'Reference Downcasting' का अर्थ/मान 'Supertype reference to subtype via (SubClass)' है (Throws ClassCastException if heap object mismatches; guard with instanceof)।
Type Casting in Java: Widening (Implicit) vs Narrowing (Explicit) — Exam FAQs
Q1: Why is float considered 'wider' than long even though long is 64 bits and float is 32 bits?
long के 64 बिट और float के 32 बिट होने के बावजूद float को long से 'wider' (व्यापक) क्यों माना जाता है?
[EN] In IEEE 754 floating-point, exponent bits enable float to represent magnitudes up to roughly 3.4 x 10^38, far exceeding long's maximum value (~9.22 x 10^18). Therefore long -> float is a widening conversion in JLS, though with potential precision loss.
[HI] IEEE 754 में एक्सपोनेंट बिट्स के कारण float ~3.4 x 10^38 तक के बड़े परिमाण व्यक्त कर सकता है जो long (~9.22 x 10^18) से बहुत अधिक है। इसलिए JLS में long -> float वाइडनिंग माना जाता है।
Q2: What is the difference between Type Casting and Type Conversion (Coercion)?
Type Casting और Type Conversion में क्या अंतर है?
[EN] Type conversion (widening) is performed automatically by the compiler whenever a smaller compatible type moves to a larger type without syntax. Type casting (narrowing) is explicitly forced by the programmer using the (targetType) cast operator.
[HI] टाइप कन्वर्ज़न (वाइडनिंग) कंपाइलर द्वारा स्वतः किया जाता है जब छोटा प्रकार बड़े संगत प्रकार में जाता है। टाइप कास्टिंग (नैरोइंग) प्रोग्रामर द्वारा स्पष्ट रूप से (targetType) ऑपरेटर लगाकर की जाती है।
Q3: Does Boxing/Unboxing (like Integer to int) count as type casting?
क्या Boxing/Unboxing (जैसे Integer से int) को टाइप कास्टिंग माना जाता है?
[EN] No; Autoboxing (int -> Integer) and Unboxing (Integer -> int) are Boxing/Unboxing Conversions introduced in Java 5. You still cannot cast int[] directly to Integer[] because arrays of primitives and wrappers are distinct types.
[HI] नहीं; Java 5 में आया Autoboxing/Unboxing प्रिमिटिव और रैपर क्लास के बीच रूपांतरण है। साथ ही int[] को सीधे Integer[] में कास्ट नहीं किया जा सकता।
Post a Comment
Post a Comment