javaä¸StringãStringBufferãStringBuilderçåºå«
javaä¸StringãStringBufferãStringBuilderæ¯ç¼ç¨ä¸ç»å¸¸ä½¿ç¨çå符串类ï¼ä»ä»¬ä¹é´çåºå«ä¹æ¯ç»å¸¸å¨é¢è¯ä¸ä¼é®å°çé®é¢ãç°å¨æ»ç»ä¸ä¸ï¼ççä»ä»¬çä¸åä¸ç¸åã
1.å¯åä¸ä¸å¯å
ããStringç±»ä¸ä½¿ç¨å符æ°ç»ä¿åå符串ï¼å¦ä¸å°±æ¯ï¼å 为æâfinalâ修饰符ï¼æ以å¯ä»¥ç¥éstring对象æ¯ä¸å¯åçã
ããããprivate final char value[];
ããStringBuilderä¸StringBufferé½ç»§æ¿èªAbstractStringBuilderç±»ï¼å¨AbstractStringBuilderä¸ä¹æ¯ä½¿ç¨å符æ°ç»ä¿åå符串ï¼å¦ä¸å°±æ¯ï¼å¯ç¥è¿ä¸¤ç§å¯¹è±¡é½æ¯å¯åçã
ããããchar[] value;
2.æ¯å¦å¤çº¿ç¨å®å
¨
ããStringä¸ç对象æ¯ä¸å¯åçï¼ä¹å°±å¯ä»¥ç解为常éï¼æ¾ç¶çº¿ç¨å®å
¨ã
ããAbstractStringBuilderæ¯StringBuilderä¸StringBufferçå
Œ
±ç¶ç±»ï¼å®ä¹äºä¸äºå符串çåºæ¬æä½ï¼å¦expandCapacityãappendãinsertãindexOfçå
Œ
±æ¹æ³ã
ããStringBuffer对æ¹æ³å äºåæ¥éæè
对è°ç¨çæ¹æ³å äºåæ¥éï¼æ以æ¯çº¿ç¨å®å
¨çãçå¦ä¸æºç ï¼
1 public synchronized StringBuffer reverse() {
2 super.reverse();
3 return this;
4 }
5
6 public int indexOf(String str) {
7 return indexOf(str, 0); //åå¨ public synchronized int indexOf(String str, int fromIndex) æ¹æ³
8 }
ããStringBuilder并没æ对æ¹æ³è¿è¡å åæ¥éï¼æ以æ¯é线ç¨å®å
¨çã
3.StringBuilderä¸StringBufferå
±åç¹
ããStringBuilderä¸StringBufferæå
Œ
±ç¶ç±»AbstractStringBuilder(æ½è±¡ç±»)ã
ããæ½è±¡ç±»ä¸æ¥å£çå
¶ä¸ä¸ä¸ªåºå«æ¯ï¼æ½è±¡ç±»ä¸å¯ä»¥å®ä¹ä¸äºåç±»çå
Œ
±æ¹æ³ï¼åç±»åªéè¦å¢å æ°çåè½ï¼ä¸éè¦éå¤åå·²ç»åå¨çæ¹æ³ï¼èæ¥å£ä¸åªæ¯å¯¹æ¹æ³çç³æå常éçå®ä¹ã
ããStringBuilderãStringBufferçæ¹æ³é½ä¼è°ç¨AbstractStringBuilderä¸çå
Œ
±æ¹æ³ï¼å¦super.append(...)ãåªæ¯StringBufferä¼å¨æ¹æ³ä¸å synchronizedå
³é®åï¼è¿è¡åæ¥ã
ããæåï¼å¦æç¨åºä¸æ¯å¤çº¿ç¨çï¼é£ä¹ä½¿ç¨StringBuilderæçé«äºStringBufferã
温馨提示:答案为网友推荐,仅供参考