Qhov twg zoo dua StringBuffer lossis StringBuilder?
Qhov twg zoo dua StringBuffer lossis StringBuilder?

Video: Qhov twg zoo dua StringBuffer lossis StringBuilder?

Video: Qhov twg zoo dua StringBuffer lossis StringBuilder?
Video: kawm sau website thiab youtube qhov twg zoo dua 2024, Hlis ntuj nqeg
Anonim

Txoj hlua yog immutable whereas StringBuffer thiab StringBuider yog cov chav kawm hloov tau. StringBuffer yog xov zoo thiab synchronized whereas StringBuilder tsis yog, yog vim li cas StringBuilder yog nrawm dua StringBuffer . Txoj hlua concat + tus neeg teb xov tooj siv sab hauv StringBuffer lossis StringBuilder chav kawm.

Raws li txoj cai, qhov twg yog qhov zoo dua StringBuffer lossis StringBuilder?

Vim li ntawd, StringBuilder yog sai dua StringBuffer . StringBuffer yog mutable. Nws tuaj yeem hloov pauv ntawm qhov ntev thiab cov ntsiab lus. StringBuffers yog xov-kev nyab xeeb, txhais tau hais tias lawv muaj synchronized txoj kev los tswj kev nkag tau tsuas yog ib xov tuaj yeem nkag mus rau a StringBuffer object's synchronized code ntawm ib lub sij hawm.

Ib yam li ntawd, vim li cas StringBuffer qeeb dua StringBuilder? Cov khoom tsim los ntawm StringBuffer yog khaws cia rau hauv lub heap. StringBuffer muaj tib txoj kev raws li cov StringBuilder , tab sis txhua txoj hauv kev StringBuffer yog synchronized uas yog StringBuffer yog xov zoo. Yog li StringBuilder sai dua tshaj tus StringBuffer thaum hu tib txoj kev ntawm txhua chav kawm.

Yog li ntawd, qhov txawv ntawm StringBuffer thiab StringBuilder yog dab tsi?

StringBuilder . StringBuilder yog tib yam li cov StringBuffer , uas yog nws khaws cov khoom hauv heap thiab nws kuj tuaj yeem hloov kho. Lub ntsiab qhov sib txawv ntawm tus StringBuffer thiab StringBuilder yog tias StringBuilder tsis muaj xov zoo. StringBuilder yog ceev ceev raws li nws tsis yog xov zoo.

Thaum twg kuv yuav tsum siv StringBuffer?

Yog tias qhov khoom muaj nqis tuaj yeem hloov pauv thiab tsuas yog nkag los ntawm ib qho xov nkaus xwb, siv StringBuilder vim StringBuilder yog unsynchronized. Nyob rau hauv rooj plaub uas tus nqi khoom tuaj yeem hloov pauv, thiab yuav raug hloov los ntawm ntau cov xov, siv a StringBuffer vim StringBuffer yog synchronized.

Pom zoo: