Qhov txawv ntawm Wrapper Class thiab Primitive Hom hauv Java

Cov txheej txheem:

Qhov txawv ntawm Wrapper Class thiab Primitive Hom hauv Java
Qhov txawv ntawm Wrapper Class thiab Primitive Hom hauv Java

Video: Qhov txawv ntawm Wrapper Class thiab Primitive Hom hauv Java

Video: Qhov txawv ntawm Wrapper Class thiab Primitive Hom hauv Java
Video: What is the Difference Between a Pointer and a Reference C++ 2024, Hlis ntuj nqeg
Anonim

Qhov Tseem Ceeb Qhov txawv - Wrapper Class vs Primitive Type in Java

Java yog hom lus nrov programming uas siv los tsim ntau yam kev siv. Ib qho txiaj ntsig ntawm Java yog tias nws txhawb nqa Object Oriented Programming (OOP). Siv OOP, qhov program lossis software tuaj yeem ua qauv siv cov khoom. Ib chav kawm yog siv los ua ib daim phiaj los tsim ib yam khoom. Hauv programming, nws yog qhov yuav tsum tau khaws cov ntaub ntawv. Reserved nco qhov chaw khaws cov ntaub ntawv yog hu ua variables. Txhua qhov sib txawv muaj cov ntaub ntawv tshwj xeeb. Muaj yim yam tseem ceeb uas muab los ntawm Java lus. Lawv yog luv, byte, int, ntab, ob, char, boolean. Qee zaum, nws yuav tsum tau hloov cov hom qub rau ib qho khoom thiab cov khoom rov qab mus rau hom qub. Cov chav kawm wrapper yog siv rau qhov kev hloov pauv no. Kab lus no tham txog qhov sib txawv ntawm chav kawm wrapper thiab hom qub hauv Java. Qhov sib txawv tseem ceeb ntawm cov chav kawm wrapper thiab hom qub hauv Java yog cov chav kawm wrapper yog siv los hloov hom qub rau ib yam khoom thiab cov khoom rov qab mus rau hom qub thaum hom qub yog hom ntaub ntawv ua ntej muab los ntawm Java programming lus.

Wrapper Class hauv Java yog dab tsi?

Ib chav kawm Wrapper hauv Java yog siv los hloov cov ntaub ntawv keeb kwm yav dhau los rau ib yam khoom thiab khoom rau hom qub. Txawm tias cov ntaub ntawv keeb kwm yav dhau los tau siv los khaws cov ntaub ntawv thawj hom, cov ntaub ntawv tsim xws li Array Lists thiab Vectors khaws cov khoom. Yog li ntawd, nws yuav tsum tau siv wrapper chav kawm rau kev hloov dua siab tshiab. Cov chav kawm wrapper sib raug rau cov hom qub char, byte, luv thiab int yog Character, Byte, Short, thiab Integer. Cov chav kawm wrapper sib raug rau ntev, ntab, ob npaug thiab boolean yog Ntev, Float, Ob thiab Boolean.

Qhov txawv ntawm Wrapper Class thiab Primitive Type in Java
Qhov txawv ntawm Wrapper Class thiab Primitive Type in Java

Daim duab 01: Java Program uas hloov cov Wrapper Classes mus rau Yam Tseem Ceeb

Raws li cov kev pab cuam saum toj no, intobj yog Integer wrapper class object. Floatobj yog Float wrapper class object. Lub doubleobj yog ob chav wrapper khoom. Cov khoom Integer tau hloov mus rau hauv int primitive siv intValue (). Ib yam li ntawd, cov khoom Float tau hloov mus rau hauv qhov qub ntab uas siv floatValue(). Ob chav khoom yog hloov dua siab tshiab rau hauv thawj ob zaug siv doubleValue (). Yog hais tias tus programmer sau cov nqe lus raws li int i=inbj; lub compiler internally sau inbj. Value(). Cov txheej txheem ntawm kev hloov pauv ib yam khoom ntawm chav kawm wrapper rau nws cov hom qub qub yog hu ua unboxing. Cov ntawv sau xws li ArrayLists siv Wrapper class vim lawv khaws cov khoom.

Hom Primitive hauv Java yog dab tsi?

Cov ntaub ntawv keeb kwm yav dhau los yog cov ntaub ntawv teev tseg ua ntej muab los ntawm Java programming lus. Muaj yim yam primitive. Lawv yog byte, luv, int, ntev, ntab, ob, boolean thiab char. Cov ntaub ntawv byte yog siv los khaws ib qho 8-ntsis kos npe ob tus lej ntxiv. Cov ntaub ntawv luv luv yog siv los khaws 16-ntsis kos npe ob tus lej ntxiv. Ib hom ntaub ntawv int yog siv los khaws 32-ntsis kos npe ob qhov sib ntxiv ua ke thaum cov ntaub ntawv ntev yog siv los khaws 64-ntsis hu nkauj ob qhov sib ntxiv ua ke. Lub ntab yog siv los khaws ib qho precision 32-ntsis ntab taw tes tus nqi thiab ob npaug yog siv los khaws ob npaug precision 64-ntsis ntab taw tes tus nqi. Lub boolean yog siv los sawv cev qhov tseeb lossis tsis tseeb. Lub char yog siv los khaws ib tus cim. Cov no yog yim yam tseem ceeb hauv Java.

Qhov tseem ceeb sib txawv ntawm Wrapper Class thiab Primitive Type in Java
Qhov tseem ceeb sib txawv ntawm Wrapper Class thiab Primitive Type in Java

Daim duab 02: Java Program uas hloov cov Hom Phiaj Nrab rau Wrapper Classes

Raws li cov kev pab cuam saum toj no, num1 yog hom int. Nws yog hloov dua siab tshiab rau hauv ib qho Integer los ntawm kev dhau num1 rau Integer.valueOf(). Float1 tuaj yeem khaws cov txiaj ntsig ntab. Nws yog hloov dua siab tshiab rau hauv hom Float los ntawm kev hla float1 rau hauv Float.valueOf(). Ib yam li ntawd, double1 tuaj yeem khaws ob qhov txiaj ntsig. Nws yog hloov dua siab tshiab rau hauv ob hom los ntawm kev dhau double1 rau hauv Double.valueOf(). Yog hais tias tus programmer sau cov nqe lus raws li Interger intobj=num1; lub compiler sau Integer.valueOf(num1); Cov txheej txheem ntawm kev hloov cov hom qub rau cov khoom siv wrapper cov chav kawm tau txais lub npe hu ua autoboxing.

Dab tsi yog qhov zoo sib xws ntawm Wrapper Class thiab Primitive Hom hauv Java?

Ob chav Wrapper thiab Primitive Hom hauv Java tuaj yeem siv los khaws cov ntaub ntawv hauv programming

Dab tsi yog qhov txawv ntawm Wrapper Class thiab Primitive Hom hauv Java?

Wrapper Class vs Primitive Type in Java

Wrapper chav kawm muab cov txheej txheem los hloov hom qub rau hauv cov khoom thiab cov khoom rau hauv hom qub. Ib hom thawj zaug yog hom ntaub ntawv teev tseg ua ntej muab los ntawm Java.
Sib Koom Tes
Ib chav Wrapper yog siv los tsim ib qho khoom; yog li ntawd, nws muaj chav kawm sib xws. Ib hom Primitive tsis yog yam khoom yog li nws tsis nyob hauv chav kawm.
Null Values
Cov khoom hauv chav kawm wrapper tso cai tsis muaj nuj nqis. Ib hom ntaub ntawv keeb kwm tsis pub muaj nuj nqis.
Nco ntsoov
Yuav tsum nco ntsoov siab dua cov hom qub. Cov Clustered Index tsis xav tau qhov chaw ntxiv. Yuav tsum nco ntsoov qis dua piv rau cov chav kawm wrapper.
Collections
Ib chav Wrapper tuaj yeem siv nrog cov khoom sau xws li ArrayList, thiab lwm yam. Ib hom thawj tsis siv nrog kev sau.

Summary – Wrapper Class vs Primitive Type in Java

Java lus muab yim yam ntaub ntawv tseem ceeb. Qee lub sij hawm nws yuav tsum tau hloov cov hom qub rau qhov khoom thiab tseem yuav hloov cov khoom rov qab mus rau primitives. Cov chav kawm Wrapper tuaj yeem siv los ua tiav txoj haujlwm ntawd. Qhov txawv ntawm wrapper class thiab primitive type hauv Java yog cov chav kawm wrapper yog siv los hloov hom qub rau ib yam khoom thiab cov khoom rov qab mus rau hom qub thaum hom qub yog hom ntaub ntawv ua ntej muab los ntawm Java programming lus.

Pom zoo: