Type Casting

Widening conversions automatically happen છે, narrowing ને explicit cast જોઈએ છે જે round નથી પણ chop કરે છે: એ reason કે BookBridge નો issue rate 0 print કર્યો અને તેને કેવી રીતે fix કરવું.

10 min read · 10 cards · 2 checks

Read in: English · हिन्दी · ગુજરાતી


Theory

report જે 0% કહ્યું

મહિનાનો અંત, BookBridge તેનો proudest number print કરે છે: library નો કેટલો ભાગ use થયો.

int issued = 47, total = 60;

System.out.println(issued / total * 100);

Output: 0. librarian impressed નથી. 47 of 60 books બહાર ગયા, એ clearly three quarters થી વધુ છે, અને તમારો report zero claim કરે છે.

કંઈ broken નથી. Java એ exactly એ કર્યું જે types એ તેને કરવા કહ્યું, અને આ lesson તેને otherwise tell કરવાનું શીખવા વિશે છે.

Theory

વેસેલ્સ વચ્ચે pouring

નાના cup ને મોટા bucket માં pour કરો: કંઈ spill નથી થતું, કોઈ permission needed નથી. એ widening છે: નાનો type મોટામાં flowing, automatic અને safe.

bucket ને cup માં pour કરો: કંઈ spill થઈ શકે છે, એટલે Java refuse કરે છે જ્યાં સુધી તમે explicitly કહો નહીં "I accept the spillage". એ narrowing છે, અને explicit cast (int) એ તમે waiver ને sign કરો છો.

Theory

Widening અને narrowing, ઔપચારિક રીતે

Widening (implicit) conversion: Java automatically નાના type ને મોટામાં convert કરે છે, કોઈ data loss વગર:

byte → short → int → long → float → double (char ladder માં int પર join થાય છે)

એટલે long isbn = 42; અને double price = 350; just work કરે છે.

Narrowing (explicit) conversion: ladder ને નીચે જવાથી data lose થવાનું risk છે, એટલે તે ફક્ત cast operator સાથે compile થાય છે: int rupees = (int) 350.75; 350 store કરે છે. cast એ promise છે કે તમે જે કંઈ lost થાય તે 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: cast ની જરૂર છે
        System.out.println(rupees);    // 350 (chopped, rounded નહીં)

        long big = issued;             // widening: automatic
        char section = 'C';
        int code = section;            // char int માં widens થાય છે: 67
        System.out.println(code);
    }
}

Theory

શા માટે report એ 0 કહ્યું

Java issued / total * 100 ને left to right evaluate કરે છે, દરેક step પર operands ના types ને use કરીને.

issued / total એ int divided by int છે, અને integer division ફક્ત whole part ને retain કરે છે: 47 / 60 = 0. પછી 0 * 100 = 0.

fix એ ONE operand ને division પહેલાં cast કરે છે: (double) issued / total એ 47.0 / 60 = 0.7833... બનાવે છે, અને * 100 એ 78.33 આપે છે. whole result ને instead cast કરવું, (double)(issued / total), એ classic wrong fix છે: damage already brackets ની અંદર થઈ ગઈ.

Quiz

System.out.println((int) 7.9); શું print કરે છે?

  1. 7
  2. 8
  3. 7.9
  4. Compile error: double ને int નથી બનાવી શકાતો
Show the answer

7

Narrowing cast truncate કરે છે: તે fractional part ને chop off કરે છે અને 7 retain કરે છે. તે ક્યારેય round નથી કરતું, એટલે 8 એ trap છે students માટે જે mathematical rounding ને assume કરે છે. 7.9 એટલે cast એ કંઈ કર્યું નહીં. અને option D એ backwards છે: double થી int WITH explicit cast એ exactly એ છે જે cast operator માટે છે; તે version WITHOUT cast છે જે compile થવા refuse કરે છે.

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 ને retain કરે છે, અને higher bits ને lose કરવાથી number wrap around થાય છે: 130 એ 127, maximum, થી 2 steps past છે, એટલે તે negative end ના 2 steps પર lands થાય છે: 130 - 256 = -126. તમારે most exams માં bit-level working ની જરૂર નથી; યાદ રાખવું out-of-range casts wrap around to strange values એ point છે: cast fit ને force કરે છે, તે check નથી કરતું.

Watch out

3 casting traps

Too late casting: (double)(47 / 60) એ 0.0 છે; int division already 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 એ first lesson નો "robust" property છે જે તેનું job કરે છે.

Theory

જ્યાં casting returns થાય છે

આ same idea છે જે BCA104 એ C માટે શીખવ્યું, Java એને more strictly enforce કરે છે. vocabulary ને sharp રાખો: widening implicit છે, narrowing explicit છે. word cast itself Unit 2 માં bigger role સાથે returns થાય છે: એકવાર BookBridge પાસે Member અને Librarian children સાથે Person parent class હોય, તમે object references ને તે family tree પર up અને down cast કરશો, અને up-is-free, down-needs-care rule આ lesson જેવું જ feel થશે.

Summary

Key takeaways

  • Widening (નાના થી મોટા: byte, short, int, long, float, double; char int પર join થાય છે) automatic અને lossless છે.
  • Narrowing (મોટા થી નાના) ને explicit cast જોઈએ છે: int rupees = (int) 350.75;
  • Casts truncate કરે છે, ક્યારેય round નથી: (int) 7.9 એ 7 છે.
  • int / int એ integer division છે; result ને નહીં, operand ને cast કરો: (double) issued / total.
  • Out-of-range casts wrap around: (byte) 130 એ -126 છે.
  • Java implicit narrowing ને compile time પર refuse કરે છે; C એ silently tolerate કર્યું.
  • Memory hook: નાના થી મોટા free flows છે, મોટા થી નાના તમે waiver sign કરો છો.

Study this properly

This page is the lesson to read. In Gri-Learn the same topic is a graded deck: the self-checks are scored and your weak topics are tracked. Free to start.

Start this topic

Already have an account? Sign in

More from Introduction to Java

Gri-Learn · syllabus-mapped B.C.A. lessons in English, Hindi and Gujarati

Type Casting · Java Programming Language · Gri-Learn