Puas yog StringBuilder sai dua txoj hlua sib txuas?
Puas yog StringBuilder sai dua txoj hlua sib txuas?

Video: Puas yog StringBuilder sai dua txoj hlua sib txuas?

Video: Puas yog StringBuilder sai dua txoj hlua sib txuas?
Video: "Puag Yoj Mas" [OFFICIAL MUSIC VIDEO] - KT Killing Em 2024, Kaum ib hlis
Anonim

1) Cov Txoj hlua object yog immutable nyob rau hauv Java tab sis StringBuffer thiab StringBuilder yog cov khoom sib hloov. 2) StringBuffer yog synchronized thaum StringBuilder tsis yog qhov ua StringBuilder sai dua StringBuffer. 3) Concatenation tus neeg teb xov tooj "+" yog siv sab hauv siv StringBuffer lossis StringBuilder.

Ib yam li ntawd ib tug yuav nug, yog txoj hlua hom ntawv sai dua concatenation?

Lub ntsiab yog vim li cas Txoj hlua . hom ntawv () tuaj yeem yooj yim dua hauv zos nrog cov ntawv sau los ntawm cov ntaub ntawv peev txheej whereas kev sib txuas tsis tuaj yeem nyob hauv cheeb tsam yam tsis tau tsim cov kev ua haujlwm tshiab nrog cov lej sib txawv rau txhua hom lus. Cov txiaj ntsig ntawm lub sijhawm yog raws li hauv qab no: Concatenation = 265 millisecond.

Tsis tas li ntawd, thaum twg koj yuav tsum tsis txhob siv StringBuilder? Yog li siv StringBuilder thaum koj xav tau ua ntau kev hloov kho ntawm txoj hlua. Tsis yog tiag koj yuav tsum siv StringBuilder Yog hais tias koj concatenate loj hlua los yog koj muaj ntau concatenations, zoo li nyob rau hauv ib lub voj. Kuv feem ntau siv txoj hlua builder rau ib qho thaiv ntawm cov cai uas yuav ua rau muaj kev sib txuas ntawm peb lossis ntau txoj hlua.

Ib yam li ntawd, uas yog txoj hlua nrawm dua lossis StringBuilder?

Cov khoom ntawm Txoj hlua yog immutable, thiab cov khoom ntawm StringBuffer thiab StringBuilder yog mutable. StringBuffer thiab StringBuilder zoo ib yam, tab sis StringBuilder yog sai dua thiab nyiam dua StringBuffer rau ib txoj haujlwm threaded. Yog xav tau xov kev ruaj ntseg, ces siv StringBuffer.

Thaum twg tus programmer yuav siv txoj hlua vs StringBuilder?

Thaum twg siv koj: if a txoj hlua yog yuav nyob tas mus li thoob plaws hauv qhov kev pab cuam, ces siv String class object vim a Txoj hlua cov khoom yog immutable. Yog ib txoj hlua tau hloov (piv txwv: ntau logic thiab kev ua haujlwm hauv kev tsim kho ntawm lub txoj hlua ) ces siv a StringBuilder yog qhov kev xaiv zoo tshaj plaws.

Pom zoo: