Cov txheej txheem:

Casting ua dab tsi hauv Java?
Casting ua dab tsi hauv Java?

Video: Casting ua dab tsi hauv Java?

Video: Casting ua dab tsi hauv Java?
Video: Dab tuag muab Xyooj Tsu nyiav ciaj 10/3102020 2024, Kaum ib hlis
Anonim

Casting hauv Java . Casting yog tus txheej txheem ntawm kev ua ib tug txawv txav behaves raws li ib tug variable ntawm lwm yam. Yog ib chav kawm share ib YOG -A lossis kev sib raug zoo nrog lwm chav kawm lossis kev sib txuas, lawv qhov hloov pauv ua tau ua pov tseg rau ib leeg yam. Qee lub sij hawm cov cast yog tso cai thiab qee lub sij hawm uas cast yog tsis pub.

Tom qab ntawd, hom casting ua haujlwm li cas hauv Java?

Hauv Java, muaj ob hom casting:

  • Widening Casting (automatically) - hloov hom me me mus rau hom loj dua. byte -> short -> char -> int -> long -> float -> double.
  • Narrowing Casting (manually) - hloov hom loj rau ib hom me me. double -> float -> long -> int -> char -> short -> byte.

Kuj Paub, ua li cas koj pov qhov sib txawv hauv Java? Hloov pauv

  1. Hom Casting hauv Java. Hom casting yog siv los hloov cov khoom lossis qhov sib txawv ntawm ib hom mus rau lwm qhov.
  2. Syntax. dataType variableName = (dataType) variableToConvert;
  3. Sau ntawv. Muaj ob txoj kev qhia casting: nqaim (loj dua rau hom me) thiab nthuav dav (me me rau hom loj).
  4. Piv txwv.

Ib yam li ntawd, nws tau nug, dab tsi yog qhov qub castings hauv Java thiab vim li cas peb xav tau?

Casting nruab nrab keeb kwm hom enables koj hloov tus nqi ntawm ib hom mus rau lwm tus keeb kwm hom. Qhov no feem ntau tshwm sim nrog cov zauv hom. Tab sis ib keeb kwm hom tsis tuaj yeem siv rau hauv lub cam khwb cia. Boolean tus nqi yuav tsum yuav yog qhov tseeb lossis tsis tseeb thiab siv tsis tau rau hauv a casting kev ua haujlwm.

Dab tsi yog hom kev hloov pauv implicit?

Implicit hom conversion yog ib qho tsis siv neeg hom kev hloov pauv ua los ntawm lub compiler thaum twg cov ntaub ntawv los ntawm txawv hom yog intermixed. Thaum ib implicit hloov dua siab tshiab ua tiav, nws tsis yog tsuas yog rov txhais cov lus qhia tus nqi tab sis a hloov dua siab tshiab ntawm tus nqi ntawd rau tus nqi sib npaug hauv qhov tshiab hom.

Pom zoo: