Qhov txawv ntawm StringBuffer thiab StringBuilder

Qhov txawv ntawm StringBuffer thiab StringBuilder
Qhov txawv ntawm StringBuffer thiab StringBuilder

Video: Qhov txawv ntawm StringBuffer thiab StringBuilder

Video: Qhov txawv ntawm StringBuffer thiab StringBuilder
Video: Qhov Txawv Ntawm Lus Hmoob Thiab Lus Mab Mawb 2024, Kaum ib hlis
Anonim

StringBuffer vs StringBuilder

Java yog ib hom lus nrov heev. Hauv Java, chav kawm String yog muab los tuav ib ntu ntawm cov cim uas tsis tuaj yeem hloov kho (ib zaug pib). Xwb, Java programming lus muab ob hom kev hloov pauv ntawm cov cim. Ntawd yog, thaum cov programmers yuav tsum hloov kho ib qho String (tom qab pib), lawv yuav tsum siv cov chav kawm StringBuffer lossis chav kawm StringBuilder, tsis yog chav kawm String. StringBuffer tau qhia hauv JDK 1.0 thiab StringBuilder chav kawm tau qhia hauv JDK 1.5, ua tau raws li kev hloov rau StringBuffer chav kawm (rau ib lub xov tooj ib puag ncig).

StringBuffer yog dab tsi?

StringBuffer chav kawm tau qhia hauv JDK 1.0. StringBuffer chav kawm belongs rau java.lang pob thiab tau txais los ntawm java.lang.object generic. Programmers tsis tuaj yeem ncua nws ntxiv vim Nws yog chav kawm kawg. StringBuffer chav kawm siv Serializable, Appendable thiab CharSequience interfaces. Ib yam khoom ntawm chav kawm StringBuffer tuaj yeem tuav ib ntu ntawm cov cim uas hloov pauv tau thiab xov zoo. Qhov ntawd txhais tau hais tias, nws zoo ib yam li String khoom, tab sis qhov sib lawv liag ntawm cov cim (ntev thiab cov ntsiab lus) tuaj yeem hloov tau txhua lub sijhawm tom qab pib qhov khoom StringBuffer. Txawm li cas los xij, qhov ntawd yuav tsum tau ua tiav siv cov txheej txheem tshwj xeeb muab los ntawm chav kawm StringBuffer. Muaj ob txoj hauv kev ua haujlwm hauv chav kawm StringBuffer. Lawv muab los ntawm append() thiab insert() txoj kev. Cov txheej txheem no yog overloaded, yog li lawv muaj peev xwm txais cov ntaub ntawv ntawm txhua yam xws li integer thiab ntev. Ob txoj hauv kev ua ntej hloov cov tswv yim rau ib txoj hlua, thiab tom qab ntawd ntxiv (appends lossis inserts) cov cim ntawm cov hlua sib txuas rau cov khoom Stribbuffer uas twb muaj lawm. Txoj kev append() ntxiv txoj hlua hloov dua siab tshiab rau qhov kawg ntawm cov khoom StringBuffer uas twb muaj lawm, thaum insert() txoj kev yuav ntxiv cov cim nkag mus rau qhov teev cov ntsiab lus ntxig.

StringBuilder yog dab tsi?

StringBuilder chav kawm tau qhia hauv JDK 1.5. StringBuilder API zoo ib yam li StringBuffer API. Qhov tseeb, StringBuilder chav kawm tau qhia ua qhov hloov pauv rau chav kawm StringBuffer (rau cov ntawv thov ib leeg). StringBuilder chav kawm belongs rau java.lang pob thiab tau txais los ntawm java.lang.object generic. Nws yog chav kawm kawg thiab yog li cov programmers tsis tuaj yeem txuas ntxiv. StringBuilder chav kawm siv Serializable, Appendable thiab CharSequience interfaces. Ib yam khoom ntawm chav kawm StringBuilder tuaj yeem tuav cov cim ntawm cov cim uas hloov tau tab sis tsis muaj xov zoo. Qhov ntawd txhais tau tias, nws zoo ib yam li String khoom, tab sis txoj hlua tuaj yeem hloov tau txhua lub sijhawm. Tab sis StringBuilder chav kawm tsis muab synchronization, thiab yog li ntawd tau thov kom ceev dua li siv StringBuffer chav kawm. StringBuilder chav kawm muab ntxiv () thiab ntxig () txoj hauv kev nrog kev ua haujlwm zoo ib yam li hauv chav kawm StringBuffer.

Qhov txawv ntawm StringBuffer thiab StringBuilder yog dab tsi?

Txawm hais tias, StringBuilder thiab StringBuffer cov chav kawm tuaj yeem siv tau rau kev sib hloov ntawm cov cim hauv Java, lawv muaj qhov sib txawv tseem ceeb. Tsis zoo li cov chav kawm StringBuffer, chav kawm StringBuilder tsis muaj xov zoo, thiab tsis muaj synchronization. Yog li ntawd, nws raug pom zoo tias cov chav kawm StringBuilder yuav tsum tau siv nyob rau hauv qhov chaw ntawm StringBuffer chav kawm hauv cov ntawv thov ib leeg, vim nws tau thov tias chav kawm StringBuilder yuav nrawm dua li StringBuffer chav kawm (nyob rau hauv ib txwm muaj xwm txheej).

Pom zoo: