Theory
वह Report जिसने 0% कहा
Month के end में, BookBridge अपना सबसे proud number print करता है: library कितनी इस्तेमाल हुई।
int issued = 47, total = 60;
System.out.println(issued / total * 100);
Output: 0। Librarian impress नहीं है। 60 में से 47 books gaye, यह clearly तीन-चौथाई से ज़्यादा है, और आपकी report zero claim करती है।
कुछ भी broken नहीं है। Java ने exactly वही किया जो types ने इसे बताया, और यह lesson इसे otherwise बताना सीखने के बारे में है।
Theory
Vessels के बीच Pour करना
एक small cup को एक big bucket में pour कीजिए: कुछ नहीं गिरता, कोई permission नहीं चाहिए। यही widening है: एक small type एक bigger एक में flow करता है, automatic और safe।
एक bucket को एक cup में pour कीजिए: कुछ spill हो सकता है, तो Java मना करता है जब तक आप explicitly न कहें "मैं spillage accept करता हूँ"। यह narrowing है, और explicit cast (int) आपके waiver sign करने जैसा है।
Theory
Widening और Narrowing, Formally
Widening (implicit) conversion: Java automatically एक smaller type को एक larger एक में convert करता है, बिना data loss के:
byte → short → int → long → float → double (char int पर ladder join करता है)
तो long isbn = 42; और double price = 350; बस काम करते हैं।
Narrowing (explicit) conversion: ladder नीचे जाना data loss का risk रखता है, तो यह सिर्फ़ एक cast operator से compile होता है: int rupees = (int) 350.75; 350 store करता है। Cast एक promise है कि आप जो कुछ भी खोता है उसे accept करते हैं।
Practical
Casts Action में (हर print predict कीजिए)
public class Casting {
public static void main(String[] args) {
int issued = 47, total = 60;
System.out.println(issued / total * 100); // 0 (int division first)
System.out.println((double) issued / total * 100); // 78.33... (fixed)
double price = 350.75;
int rupees = (int) price; // narrowing: needs the cast
System.out.println(rupees); // 350 (chopped, not rounded)
long big = issued; // widening: automatic
char section = 'C';
int code = section; // char widens to int: 67
System.out.println(code);
}
}
Theory
Report ने 0 क्यों कहा
Java issued / total * 100 को left to right evaluate करता है, हर step पर operands की types इस्तेमाल करते हुए।
issued / total int divided by int है, और integer division सिर्फ़ whole part रखता है: 47 / 60 = 0। फिर 0 * 100 = 0।
Fix division से पहले ONE operand को cast करता है: (double) issued / total इसे 47.0 / 60 = 0.7833... बनाता है, और * 100 78.33 देता है। पूरे result को cast करना, (double)(issued / total), classic wrong fix है: damage brackets के अंदर पहले ही हो चुका है।
Quiz
System.out.println((int) 7.9); क्या print करता है?
- 7
- 8
- 7.9
- Compile error: एक double एक int नहीं बन सकता
Show the answer
7
एक narrowing cast truncate करता है: यह fractional part chop कर देता है और 7 रखता है। यह कभी round नहीं करता, तो 8 उन students के लिए trap है जो mathematical rounding assume करते हैं। 7.9 का मतलब होगा cast ने कुछ नहीं किया। और option D इसे backwards रखता है: एक explicit cast के WITH double से int exactly वह है जिसके लिए cast operator है; बिना cast के version compile होने से मना करता है।
Think first
वह Byte जो Negative हो गया
byte b = (byte) 130; System.out.println(b); एक byte -128 से 127 तक hold करता है, और 130 fit नहीं होता। Tap करने से पहले: क्या print होता है?
Show the answer
-126। एक byte value के सिर्फ़ lowest 8 bits रखता है, और higher bits खोना number को wrap around कराता है: 130, 127 से 2 steps आगे है, maximum, तो यह negative end में 2 steps land करता है: 130 - 256 = -126। ज़्यादातर exams में आपको bit-level working की ज़रूरत नहीं: याद रखना कि out-of-range casts strange values में wrap around होते हैं point है: एक cast fit force करता है, यह check नहीं करता।
Watch out
3 Casting Traps
बहुत देर से Casting: (double)(47 / 60) 0.0 है; int division ने पहले ही fraction destroy कर दिया। Result को नहीं, एक operand को cast कीजिए।
Rounding Expect करना: (int) 7.9 7 है। Casts chop करते हैं।
C की Tolerance Assume करना: C में, int x = 3.5; quietly compile होता है। Java इसे एक compile error कहता है (possible lossy conversion) और cast demand करता है। Java कभी silently narrow नहीं करता; वह strictness पहले lesson की "robust" property है अपना job करते हुए।
Theory
Casting कहाँ वापस आता है
यह वही idea है जो BCA104 ने C के लिए सिखाया, Java इसे ज़्यादा strictly enforce करते हुए। Vocabulary sharp रखिए: widening implicit है, narrowing explicit है। Cast शब्द खुद Unit 2 में एक बड़े role के साथ वापस आता है: एक बार BookBridge के पास Member और Librarian children वाला एक Person parent class हो जाए, आप उस family tree को ऊपर-नीचे object references cast करेंगे, और up-is-free, down-needs-care rule exactly इस lesson जैसा feel होगा।
Summary
Key takeaways
- Widening (small से large: byte, short, int, long, float, double; char int पर join होता है) automatic और lossless है।
- Narrowing (large से small) को एक explicit cast चाहिए: int rupees = (int) 350.75;
- Casts truncate करते हैं, कभी round नहीं करते: (int) 7.9 7 है।
- int / int integer division है; bracketed result को नहीं, एक operand को cast कीजिए: (double) issued / total।
- Out-of-range casts wrap करते हैं: (byte) 130 -126 है।
- Java compile time पर implicit narrowing से मना करता है; C इसे silently tolerate करता था।
- Memory hook: small से big free flow करता है, big से small आप waiver sign करते हैं।