Qhov Sib txawv ntawm Txoj Cai StringBuffer thiab StringBuilder hauv Java

Cov txheej txheem:

Qhov Sib txawv ntawm Txoj Cai StringBuffer thiab StringBuilder hauv Java
Qhov Sib txawv ntawm Txoj Cai StringBuffer thiab StringBuilder hauv Java

Video: Qhov Sib txawv ntawm Txoj Cai StringBuffer thiab StringBuilder hauv Java

Video: Qhov Sib txawv ntawm Txoj Cai StringBuffer thiab StringBuilder hauv Java
Video: Lub Zog Nruab Nrog (Hmong Dubbed) 2024, Hlis ntuj nqeg
Anonim

Qhov Tseem Ceeb Qhov txawv - Txoj hlua vs StringBuffer vs StringBuilder hauv Java

String, StringBuffer thiab String Builder yog cov chav kawm hauv Java. String yog dav siv hauv Java programming. Thaum ib yam khoom ntawm String raug tsim, nws tsis tuaj yeem hloov lawv. Txhua zaus ib qho kev hloov tshwm sim rau String, nws tsim ib txoj hlua tshiab. Txawm hais tias nws yog concatenation rau ib txoj hlua uas twb muaj lawm nws tsim ib txoj hlua tshiab. Qhov no ua rau nco khib nyiab. StringBuffer thiab StringBuilder cov chav kawm hauv Java yog siv los hloov txoj hlua. Qhov sib txawv tseem ceeb ntawm String, StringBuffer thiab StringBuilder hauv Java yog String yog ib chav kawm los tsim ib yam khoom ntawm hom String uas yog ib qib ntawm cov cim, StringBuffer yog ib chav kawm uas siv los hloov cov hlua uas muab xov zoo, thiab StringBuilder yog ib qho chav kawm uas yog siv los hloov cov hlua uas tsis muaj kev ruaj ntseg xov.

Txoj hlua hauv Java yog dab tsi?

String class is in java.lang package. Txhua zaus tus programmer tsim ib txoj hlua, nws yog ib yam khoom ntawm hom hlua. Cov hlua yog lub ntsiab lus hloov tsis tau thaum tsim khoom, nws hloov tsis tau. Cov khoom tsim siv wrapper cov chav kawm xws li Integer, Byte, Float, Ob chav kuj hloov tsis tau. Ib txoj hlua literal yog nyob rau hauv ob nqe lus. e.g. “nyob zoo ntiaj teb”. Txhua zaus ib txoj hlua literal tsim, Java Virtual Machine (JVM) xyuas cov hlua txuas tas li. Yog tias String muaj, ib qho kev siv rau lub pas dej String tas li raug xa rov qab. Yog hais tias nws yog ib tug tshiab String, cov khoom ntawd yog tsim nyob rau hauv lub String tas li pas dej.

Qhov sib txawv ntawm txoj hlua StringBuffer thiab StringBuilder hauv Java
Qhov sib txawv ntawm txoj hlua StringBuffer thiab StringBuilder hauv Java
Qhov sib txawv ntawm txoj hlua StringBuffer thiab StringBuilder hauv Java
Qhov sib txawv ntawm txoj hlua StringBuffer thiab StringBuilder hauv Java

Daim duab 01: Java program siv txoj hlua, StringBuffer thiab StringBuilder

Saib daim ntawv hauv qab no.

String s1="Nyob zoo";

s1=s1 + “World”;

System.out.println(s1);

Nyob rau hauv thawj nqe lus, s1 yog hais txog "nyob zoo" nyob rau hauv lub String tas li pas dej. Hauv nqe lus thib ob, JVM tsis hloov txoj hlua uas twb muaj lawm. Hloov chaw, nws tsim txoj hlua tshiab li "Nyob Zoo Ntiaj Teb" thiab s1 tam sim no yog xa mus rau txoj hlua tshiab. Cov khoom tawm "Nyob Zoo" tseem muaj nyob hauv String lub pas dej tas li.

Yog muaj code uas yog, String s1=”Hello”;

String s2=s1;

s1, s2 ob leeg yuav raug xa mus rau kab khoom "Nyob zoo".

StringBuffer hauv Java yog dab tsi?

StringBuffer chav kawm yog siv los ua cov khoom String hloov tau. Yog li ntawd, cov khoom no tuaj yeem hloov kho. StringBuffer txhais plaub tus tsim. StringBuffer(), StringBuffer(int loj), StringBuffer(String str), StringBuffer (charSequence ch)

Saib cov cai hauv qab no, StringBuffer s1=tshiab StringBuffer("Hello");

s1.append(“World”);

System.out.println(s1);

Nyob rau hauv nqe lus 1, s1 yog hais txog "Nyob Zoo" khoom nyob rau hauv ib heap. Cov khoom yog mutable vim nws yog tsim siv StringBuffer. Hauv nqe lus 2, "Lub Ntiaj Teb" yog txuas ntxiv rau tib yam "Nyob Zoo" Txoj hlua khoom.

Txoj hlua khoom tsim nrog chav kawm StringBuffer tuaj yeem txuag lub cim xeeb. StringBuffer muab xov kev nyab xeeb vim tias ob txoj xov tsis tuaj yeem nkag mus rau tib txoj kev hauv chav kawm StringBuffer ib txhij. Xov kev ruaj ntseg txo StringBuffer kev ua tau zoo. StringBuffer chav kawm muaj txoj hauv kev xws li append(), ntxig(), thim rov qab(), hloov().

StringBuilder yog dab tsi hauv Java?

StringBuilder chav kawm yog siv los ua cov khoom String hloov tau. Yog li ntawd, cov khoom no tuaj yeem hloov kho. Lub functionality zoo ib yam li StringBuffer, tab sis qhov no tsis muab xov kev ruaj ntseg. StringBuilder muaj cov tsim xws li StringBuilder(), StringBuilder (int loj), StringBuilder (String str).

Refer the below code.

StringBuilder s1=new StringBuilder("Nyob zoo");

s1.append(“World”);

System.out.println(s1);

Nyob rau hauv nqe lus 1, s1 yog hais txog "Nyob Zoo" khoom nyob rau hauv ib heap. Cov khoom yog mutable vim nws yog tsim siv StringBuilder. Hauv nqe lus 2, "Lub Ntiaj Teb" yog txuas ntxiv rau tib yam "Nyob Zoo" Txoj hlua khoom. Tsis muaj kev tsim cov khoom String tshiab kiag li.

Txoj hlua khoom tsim nrog StringBuilder chav kawm tuaj yeem txuag kev nco. Tsis zoo li hauv StringBuffer, StringBuilder tsis muab xov kev nyab xeeb vim tias ob txoj xov tuaj yeem nkag mus rau tib txoj kev hauv chav StringBuilder ib txhij. StringBuilder chav kawm muaj txoj hauv kev xws li append(), ntxig(), thim rov qab(), hloov().

Dab tsi yog qhov zoo sib xws ntawm txoj hlua, StringBuffer thiab StringBuilder hauv Java?

Txhua tus siv tau los tsim cov hlua

Qhov txawv ntawm Txoj hlua StringBuffer thiab StringBuilder hauv Java yog dab tsi?

String vs StringBuffer vs StringBuilder

Txoj hlua Txoj hlua yog Java chav kawm uas yog siv los tsim cov khoom ntawm hom String, uas yog ib ntu ntawm cov cim.
StringBuffer StringBuffer yog chav kawm Java uas yog siv los tsim cov khoom siv hlua, uas tuaj yeem hloov kho nrog xov kev nyab xeeb.
StringBuilder StringBuilder yog chav kawm uas siv los tsim cov khoom siv hlua, uas tuaj yeem hloov kho yam tsis muaj xov kev nyab xeeb.
muas
Txoj hlua Txoj hlua yog chav kawm tsis hloov.
StringBuffer StringBuffer yog chav hloov pauv tau.
StringBuilder StringBuilder yog chav hloov pauv tau.
xov kev nyab xeeb
Txoj hlua Txoj hlua txoj hlua yog xov zoo.
StringBuffer Txoj kev ntawm StringBuffer yog xov-zoo thiab synchronized.
StringBuilder Txoj kev ntawm StringBuilder tsis threaded zoo thiab tsis synchronized.
Kev ua haujlwm
Txoj hlua txoj hlua nrawm.
StringBuffer StringBuffer qeeb.
StringBuilder StringBuilder nrawm.

Summary – String vs StringBuffer vs StringBuilder hauv Java

String, StringBuffer thiab StringBuilder zoo li qub, tab sis lawv muaj lub ntsiab lus sib txawv. Tag nrho cov no yog cov chav kawm Java. Qhov txawv ntawm String, StringBuffer thiab StringBuilder hauv Java yog tias, String yog chav kawm los tsim ib yam khoom ntawm hom String, uas yog ib pawg ntawm cov cim, StringBuffer yog chav kawm uas siv los hloov cov hlua thiab muab xov kev nyab xeeb, thaum StringBuilder chav kawm uas yog siv los hloov cov hlua uas tsis muaj kev ruaj ntseg xov.

Download the PDF String vs StringBuffer vs StringBuilder hauv Java

Koj tuaj yeem rub tawm PDF version ntawm kab lus no thiab siv nws rau offline hom phiaj raws li daim ntawv ceeb toom. Thov rub tawm PDF version ntawm no Qhov txawv Ntawm Txoj hlua StringBuffer thiab StringBuilder hauv Java

Pom zoo: