{"id":219124,"date":"2021-06-07T10:04:27","date_gmt":"2021-06-07T02:04:27","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=219124"},"modified":"2021-05-23T11:05:13","modified_gmt":"2021-05-23T03:05:13","slug":"introduction-to-the-7","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/introduction-to-the-7.html","title":{"rendered":"Java \u4e4bString\u3001StringBuffer \u548c StringBuilder \u4e09\u8005\u533a\u522b\u4ecb\u7ecd"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\nString\uff1a\u5b57\u7b26\u4e32\u5e38\u91cf\uff0c\u5b57\u7b26\u4e32\u957f\u5ea6\u4e0d\u53ef\u53d8\uff0cStringBuffer\uff1a\u5b57\u7b26\u4e32\u53d8\u91cf\uff08Synchronized\uff0c\u5373\u7ebf\u7a0b\u5b89\u5168\uff09\uff0cStringBuilder\uff1a\u5b57\u7b26\u4e32\u53d8\u91cf\uff08\u975e\u7ebf\u7a0b\u5b89\u5168\uff09\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\"\"<\/p>\n

String<\/strong><\/div>\n

String\uff1a\u5b57\u7b26\u4e32\u5e38\u91cf\uff0c\u5b57\u7b26\u4e32\u957f\u5ea6\u4e0d\u53ef\u53d8\u3002Java \u4e2d String \u662f immutable\uff08\u4e0d\u53ef\u53d8\uff09\u7684\u3002<\/p>\n

String \u7c7b\u7684\u5305\u542b\u5982\u4e0b\u5b9a\u4e49\uff1a<\/p>\n

\/** The value is used for character storage. *\/\r\nprivate final char value[];\r\n\r\n\/** The offset is the first index of the storage that is used. *\/\r\nprivate final int offset;\r\n\r\n\/** The count is the number of characters in the String. *\/\r\nprivate final int count;<\/pre>\n

\u7528\u4e8e\u5b58\u653e\u5b57\u7b26\u7684\u6570\u7ec4\u88ab\u58f0\u660e\u4e3a final \u7684\uff0c\u56e0\u6b64\u53ea\u80fd\u8d4b\u503c\u4e00\u6b21\uff0c\u4e0d\u53ef\u518d\u66f4\u6539\u3002<\/p>\n

StringBuffer\uff08JDK1.0\uff09<\/strong><\/div>\n

StringBuffer\uff1a\u5b57\u7b26\u4e32\u53d8\u91cf\uff08Synchronized\uff0c\u5373\u7ebf\u7a0b\u5b89\u5168\uff09\u3002\u5982\u679c\u8981\u9891\u7e41\u5bf9\u5b57\u7b26\u4e32\u5185\u5bb9\u8fdb\u884c\u4fee\u6539\uff0c\u51fa\u4e8e\u6548\u7387\u8003\u8651\u6700\u597d\u4f7f\u7528 StringBuffer\uff0c\u5982\u679c\u60f3\u8f6c\u6210 String \u7c7b\u578b\uff0c\u53ef\u4ee5\u8c03\u7528 StringBuffer \u7684 toString() \u65b9\u6cd5\u3002<\/p>\n

Java.lang.StringBuffer \u7ebf\u7a0b\u5b89\u5168\u7684\u53ef\u53d8\u5b57\u7b26\u5e8f\u5217\u3002\u5728\u4efb\u610f\u65f6\u95f4\u70b9\u4e0a\u5b83\u90fd\u5305\u542b\u67d0\u79cd\u7279\u5b9a\u7684\u5b57\u7b26\u5e8f\u5217\uff0c\u4f46\u901a\u8fc7\u67d0\u4e9b\u65b9\u6cd5\u8c03\u7528\u53ef\u4ee5\u6539\u53d8\u8be5\u5e8f\u5217\u7684\u957f\u5ea6\u548c\u5185\u5bb9\u3002\u53ef\u5c06\u5b57\u7b26\u4e32\u7f13\u51b2\u533a\u5b89\u5168\u5730\u7528\u4e8e\u591a\u4e2a\u7ebf\u7a0b\u3002<\/p>\n

StringBuffer \u4e0a\u7684\u4e3b\u8981\u64cd\u4f5c\u662f append \u548c insert \u65b9\u6cd5\uff0c\u53ef\u91cd\u8f7d\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u4ee5\u63a5\u53d7\u4efb\u610f\u7c7b\u578b\u7684\u6570\u636e\u3002\u6bcf\u4e2a\u65b9\u6cd5\u90fd\u80fd\u6709\u6548\u5730\u5c06\u7ed9\u5b9a\u7684\u6570\u636e\u8f6c\u6362\u6210\u5b57\u7b26\u4e32\uff0c\u7136\u540e\u5c06\u8be5\u5b57\u7b26\u4e32\u7684\u5b57\u7b26\u8ffd\u52a0\u6216\u63d2\u5165\u5230\u5b57\u7b26\u4e32\u7f13\u51b2\u533a\u4e2d\u3002<\/p>\n

    \n
  1. append \u65b9\u6cd5\u59cb\u7ec8\u5c06\u8fd9\u4e9b\u5b57\u7b26\u6dfb\u52a0\u5230\u7f13\u51b2\u533a\u7684\u672b\u7aef\uff1b<\/li>\n
  2. insert \u65b9\u6cd5\u5219\u5728\u6307\u5b9a\u7684\u70b9\u6dfb\u52a0\u5b57\u7b26\u3002<\/li>\n<\/ol>\n

    \u4f8b\u5982\uff0c\u5982\u679c z \u5f15\u7528\u4e00\u4e2a\u5f53\u524d\u5185\u5bb9\u662f start \u7684\u5b57\u7b26\u4e32\u7f13\u51b2\u533a\u5bf9\u8c61\uff0c\u5219\u6b64\u65b9\u6cd5\u8c03\u7528 z.append(\"le\") \u4f1a\u4f7f\u5b57\u7b26\u4e32\u7f13\u51b2\u533a\u5305\u542b startle \uff0c\u800c z.insert(4, \"le\") \u5c06\u66f4\u6539\u5b57\u7b26\u4e32\u7f13\u51b2\u533a\uff0c\u4f7f\u4e4b\u5305\u542b starlet \u3002<\/p>\n

    StringBuilder\uff08JDK5.0\uff09<\/strong><\/div>\n

    StringBuilder\uff1a\u5b57\u7b26\u4e32\u53d8\u91cf\uff08\u975e\u7ebf\u7a0b\u5b89\u5168\uff09\u3002\u5728\u5185\u90e8\uff0cStringBuilder \u5bf9\u8c61\u88ab\u5f53\u4f5c\u662f\u4e00\u4e2a\u5305\u542b\u5b57\u7b26\u5e8f\u5217\u7684\u53d8\u957f\u6570\u7ec4\u3002<\/p>\n

    java.lang.StringBuilder \u662f\u4e00\u4e2a\u53ef\u53d8\u7684\u5b57\u7b26\u5e8f\u5217\uff0c\u662f JDK5.0 \u65b0\u589e\u7684\u3002\u6b64\u7c7b\u63d0\u4f9b\u4e00\u4e2a\u4e0e StringBuffer \u517c\u5bb9\u7684 API\uff0c\u4f46\u4e0d\u4fdd\u8bc1\u540c\u6b65\u3002\u8be5\u7c7b\u88ab\u8bbe\u8ba1\u7528\u4f5c StringBuffer \u7684\u4e00\u4e2a\u7b80\u6613\u66ff\u6362\uff0c\u7528\u5728\u5b57\u7b26\u4e32\u7f13\u51b2\u533a\u88ab\u5355\u4e2a\u7ebf\u7a0b\u4f7f\u7528\u7684\u65f6\u5019\uff08\u8fd9\u79cd\u60c5\u51b5\u5f88\u666e\u904d\uff09\u3002<\/p>\n

    \u5176\u6784\u9020\u65b9\u6cd5\u5982\u4e0b\uff1a<\/p>\n\n\n\n\n\n\n\n
    \u6784\u9020\u65b9\u6cd5<\/td>\n\u63cf\u8ff0<\/td>\n<\/tr>\n
    StringBuilder()<\/td>\n\u521b\u5efa\u4e00\u4e2a\u5bb9\u91cf\u4e3a16\u7684StringBuilder\u5bf9\u8c61\uff0816\u4e2a\u7a7a\u5143\u7d20\uff09<\/td>\n<\/tr>\n
    StringBuilder(CharSequence cs)<\/td>\n\u521b\u5efa\u4e00\u4e2a\u5305\u542bcs\u7684StringBuilder\u5bf9\u8c61\uff0c\u672b\u5c3e\u9644\u52a016\u4e2a\u7a7a\u5143\u7d20<\/td>\n<\/tr>\n
    StringBuilder(int initCapacity)<\/td>\n\u521b\u5efa\u4e00\u4e2a\u5bb9\u91cf\u4e3ainitCapacity\u7684StringBuilder\u5bf9\u8c61<\/td>\n<\/tr>\n
    StringBuilder(String s)<\/td>\n\u521b\u5efa\u4e00\u4e2a\u5305\u542bs\u7684StringBuilder\u5bf9\u8c61\uff0c\u672b\u5c3e\u9644\u52a016\u4e2a\u7a7a\u5143\u7d20<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

    \u5728\u5927\u90e8\u5206\u60c5\u51b5\u4e0b\uff0cStringBuilder > StringBuffer<\/span>\u3002\u8fd9\u4e3b\u8981\u662f\u7531\u4e8e\u524d\u8005\u4e0d\u9700\u8981\u8003\u8651\u7ebf\u7a0b\u5b89\u5168\u3002<\/p>\n

    \u4e09\u8005\u533a\u522b<\/strong><\/div>\n

    String \u7c7b\u578b\u548c StringBuffer \u7684\u4e3b\u8981\u6027\u80fd\u533a\u522b\uff1aString \u662f\u4e0d\u53ef\u53d8\u7684\u5bf9\u8c61, \u56e0\u6b64\u5728\u6bcf\u6b21\u5bf9 String \u7c7b\u578b\u8fdb\u884c\u6539\u53d8\u7684\u65f6\u5019\uff0c\u90fd\u4f1a\u751f\u6210\u4e00\u4e2a\u65b0\u7684 String \u5bf9\u8c61\uff0c\u7136\u540e\u5c06\u6307\u9488\u6307\u5411\u65b0\u7684 String \u5bf9\u8c61\uff0c\u6240\u4ee5\u7ecf\u5e38\u6539\u53d8\u5185\u5bb9\u7684\u5b57\u7b26\u4e32\u6700\u597d\u4e0d\u8981\u7528 String \uff0c\u56e0\u4e3a\u6bcf\u6b21\u751f\u6210\u5bf9\u8c61\u90fd\u4f1a\u5bf9\u7cfb\u7edf\u6027\u80fd\u4ea7\u751f\u5f71\u54cd\uff0c\u7279\u522b\u5f53\u5185\u5b58\u4e2d\u65e0\u5f15\u7528\u5bf9\u8c61\u591a\u4e86\u4ee5\u540e\uff0c JVM \u7684 GC \u5c31\u4f1a\u5f00\u59cb\u5de5\u4f5c\uff0c\u6027\u80fd\u5c31\u4f1a\u964d\u4f4e\u3002<\/p>\n

    \u4f7f\u7528 StringBuffer \u7c7b\u65f6\uff0c\u6bcf\u6b21\u90fd\u4f1a\u5bf9 StringBuffer \u5bf9\u8c61\u672c\u8eab\u8fdb\u884c\u64cd\u4f5c\uff0c\u800c\u4e0d\u662f\u751f\u6210\u65b0\u7684\u5bf9\u8c61\u5e76\u6539\u53d8\u5bf9\u8c61\u5f15\u7528\u3002\u6240\u4ee5\u591a\u6570\u60c5\u51b5\u4e0b\u63a8\u8350\u4f7f\u7528 StringBuffer \uff0c\u7279\u522b\u662f\u5b57\u7b26\u4e32\u5bf9\u8c61\u7ecf\u5e38\u6539\u53d8\u7684\u60c5\u51b5\u4e0b\u3002<\/p>\n

    \u5728\u67d0\u4e9b\u7279\u522b\u60c5\u51b5\u4e0b\uff0c String \u5bf9\u8c61\u7684\u5b57\u7b26\u4e32\u62fc\u63a5\u5176\u5b9e\u662f\u88ab Java Compiler \u7f16\u8bd1\u6210\u4e86 StringBuffer \u5bf9\u8c61\u7684\u62fc\u63a5\uff0c\u6240\u4ee5\u8fd9\u4e9b\u65f6\u5019 String \u5bf9\u8c61\u7684\u901f\u5ea6\u5e76\u4e0d\u4f1a\u6bd4 StringBuffer \u5bf9\u8c61\u6162\uff0c\u4f8b\u5982\uff1a<\/p>\n

    String s1 = \u201cThis is only a\u201d + \u201c simple\u201d + \u201c test\u201d;\r\nStringBuffer Sb = new StringBuilder(\u201cThis is only a\u201d).append(\u201c simple\u201d).append(\u201c test\u201d);<\/pre>\n

    \u751f\u6210 String s1 \u5bf9\u8c61\u7684\u901f\u5ea6\u5e76\u4e0d\u6bd4 StringBuffer \u6162\u3002\u5176\u5b9e\u5728 Java Compiler \u91cc\uff0c\u81ea\u52a8\u505a\u4e86\u5982\u4e0b\u8f6c\u6362\uff1a<\/p>\n

    Java Compiler\u76f4\u63a5\u628a\u4e0a\u8ff0\u7b2c\u4e00\u6761\u8bed\u53e5\u7f16\u8bd1\u4e3a\uff1a<\/p>\n

    String s1 = \u201cThis is only a simple test\u201d;<\/pre>\n

    \u6240\u4ee5\u901f\u5ea6\u5f88\u5feb\u3002\u4f46\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5982\u679c\u62fc\u63a5\u7684\u5b57\u7b26\u4e32\u6765\u81ea\u53e6\u5916\u7684 String \u5bf9\u8c61\u7684\u8bdd\uff0cJava Compiler \u5c31\u4e0d\u4f1a\u81ea\u52a8\u8f6c\u6362\u4e86\uff0c\u901f\u5ea6\u4e5f\u5c31\u6ca1\u90a3\u4e48\u5feb\u4e86\uff0c\u4f8b\u5982\uff1a<\/p>\n

    String s2 = \u201cThis is only a\u201d;  \r\nString s3 = \u201c simple\u201d;  \r\nString s4 = \u201c test\u201d;  \r\nString s1 = s2 + s3 + s4;<\/pre>\n

    \u8fd9\u65f6\u5019\uff0cJava Compiler \u4f1a\u89c4\u89c4\u77e9\u77e9\u7684\u6309\u7167\u539f\u6765\u7684\u65b9\u5f0f\u53bb\u505a\uff0cString \u7684 concatenation\uff08\u5373+\uff09\u64cd\u4f5c\u5229\u7528\u4e86 StringBuilder\uff08\u6216StringBuffer\uff09\u7684append \u65b9\u6cd5\u5b9e\u73b0\uff0c\u6b64\u65f6\uff0c\u5bf9\u4e8e\u4e0a\u8ff0\u60c5\u51b5\uff0c\u82e5 s2\uff0cs3\uff0cs4 \u91c7\u7528 String \u5b9a\u4e49\uff0c\u62fc\u63a5\u65f6\u9700\u8981\u989d\u5916\u521b\u5efa\u4e00\u4e2a StringBuffer\uff08\u6216StringBuilder\uff09\uff0c\u4e4b\u540e\u5c06StringBuffer \u8f6c\u6362\u4e3a String\uff0c\u82e5\u91c7\u7528 StringBuffer\uff08\u6216StringBuilder\uff09\uff0c\u5219\u4e0d\u9700\u989d\u5916\u521b\u5efa StringBuffer\u3002<\/p>\n

    \u4f7f\u7528\u7b56\u7565<\/strong><\/div>\n

    \uff081\uff09\u57fa\u672c\u539f\u5219\uff1a\u5982\u679c\u8981\u64cd\u4f5c\u5c11\u91cf\u7684\u6570\u636e\uff0c\u7528String \uff1b\u5355\u7ebf\u7a0b\u64cd\u4f5c\u5927\u91cf\u6570\u636e\uff0c\u7528StringBuilder \uff1b\u591a\u7ebf\u7a0b\u64cd\u4f5c\u5927\u91cf\u6570\u636e\uff0c\u7528StringBuffer\u3002<\/p>\n

    \uff082\uff09\u4e0d\u8981\u4f7f\u7528String\u7c7b\u7684\"+\"\u6765\u8fdb\u884c\u9891\u7e41\u7684\u62fc\u63a5\uff0c\u56e0\u4e3a\u90a3\u6837\u7684\u6027\u80fd\u6781\u5dee\u7684\uff0c\u5e94\u8be5\u4f7f\u7528StringBuffer\u6216StringBuilder\u7c7b\uff0c\u8fd9\u5728Java\u7684\u4f18\u5316\u4e0a\u662f\u4e00\u6761\u6bd4\u8f83\u91cd\u8981\u7684\u539f\u5219\u3002\u4f8b\u5982\uff1a<\/p>\n

    String result = \"\";\r\nfor (String s : hugeArray) {\r\n    result = result + s;\r\n}\r\n\r\n\/\/ \u4f7f\u7528StringBuilder\r\nStringBuilder sb = new StringBuilder();\r\nfor (String s : hugeArray) {\r\n    sb.append(s);\r\n}\r\nString result = sb.toString();<\/pre>\n

    \u5f53\u51fa\u73b0\u4e0a\u9762\u7684\u60c5\u51b5\u65f6\uff0c\u663e\u7136\u6211\u4eec\u8981\u91c7\u7528\u7b2c\u4e8c\u79cd\u65b9\u6cd5\uff0c\u56e0\u4e3a\u7b2c\u4e00\u79cd\u65b9\u6cd5\uff0c\u6bcf\u6b21\u5faa\u73af\u90fd\u4f1a\u521b\u5efa\u4e00\u4e2aString result\u7528\u4e8e\u4fdd\u5b58\u7ed3\u679c\uff0c\u9664\u6b64\u4e4b\u5916\u4e8c\u8005\u57fa\u672c\u76f8\u540c\uff08\u5bf9\u4e8ejdk1.5\u53ca\u4e4b\u540e\u7248\u672c\uff09\u3002<\/p>\n

    \uff083\uff09\u4e3a\u4e86\u83b7\u5f97\u66f4\u597d\u7684\u6027\u80fd\uff0c\u5728\u6784\u9020 StringBuffer \u6216 StringBuilder \u65f6\u5e94\u5c3d\u53ef\u80fd\u6307\u5b9a\u5b83\u4eec\u7684\u5bb9\u91cf\u3002\u5f53\u7136\uff0c\u5982\u679c\u4f60\u64cd\u4f5c\u7684\u5b57\u7b26\u4e32\u957f\u5ea6\uff08length\uff09\u4e0d\u8d85\u8fc7 16 \u4e2a\u5b57\u7b26\u5c31\u4e0d\u7528\u4e86\uff0c\u5f53\u4e0d\u6307\u5b9a\u5bb9\u91cf\uff08capacity\uff09\u65f6\u9ed8\u8ba4\u6784\u9020\u4e00\u4e2a\u5bb9\u91cf\u4e3a16\u7684\u5bf9\u8c61\u3002\u4e0d\u6307\u5b9a\u5bb9\u91cf\u4f1a\u663e\u8457\u964d\u4f4e\u6027\u80fd\u3002<\/p>\n

    \uff084\uff09StringBuilder \u4e00\u822c\u4f7f\u7528\u5728\u65b9\u6cd5\u5185\u90e8\u6765\u5b8c\u6210\u7c7b\u4f3c + \u529f\u80fd\uff0c\u56e0\u4e3a\u662f\u7ebf\u7a0b\u4e0d\u5b89\u5168\u7684\uff0c\u6240\u4ee5\u7528\u5b8c\u4ee5\u540e\u53ef\u4ee5\u4e22\u5f03\u3002StringBuffer \u4e3b\u8981\u7528\u5728\u5168\u5c40\u53d8\u91cf\u4e2d\u3002<\/p>\n

    \uff085\uff09\u76f8\u540c\u60c5\u51b5\u4e0b\u4f7f\u7528 StringBuilder \u76f8\u6bd4\u4f7f\u7528 StringBuffer \u4ec5\u80fd\u83b7\u5f97 10%~15% \u5de6\u53f3\u7684\u6027\u80fd\u63d0\u5347\uff0c\u4f46\u5374\u8981\u5192\u591a\u7ebf\u7a0b\u4e0d\u5b89\u5168\u7684\u98ce\u9669\u3002\u800c\u5728\u73b0\u5b9e\u7684\u6a21\u5757\u5316\u7f16\u7a0b\u4e2d\uff0c\u8d1f\u8d23\u67d0\u4e00\u6a21\u5757\u7684\u7a0b\u5e8f\u5458\u4e0d\u4e00\u5b9a\u80fd\u6e05\u6670\u5730\u5224\u65ad\u8be5\u6a21\u5757\u662f\u5426\u4f1a\u653e\u5165\u591a\u7ebf\u7a0b\u7684\u73af\u5883\u4e2d\u8fd0\u884c\uff0c\u56e0\u6b64\uff1a\u9664\u975e\u786e\u5b9a\u7cfb\u7edf\u7684\u74f6\u9888\u662f\u5728 StringBuffer \u4e0a\uff0c\u5e76\u4e14\u786e\u5b9a\u4f60\u7684\u6a21\u5757\u4e0d\u4f1a\u8fd0\u884c\u5728\u591a\u7ebf\u7a0b\u6a21\u5f0f\u4e0b\uff0c\u624d\u53ef\u4ee5\u91c7\u7528 StringBuilder\uff1b\u5426\u5219\u8fd8\u662f\u7528 StringBuffer\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

    String\uff1a\u5b57\u7b26\u4e32\u5e38\u91cf\uff0c\u5b57\u7b26\u4e32\u957f\u5ea6\u4e0d\u53ef\u53d8\u3002Java \u4e2d String \u662f immutable\uff08\u4e0d\u53ef\u53d8\uff09\u7684\u3002 […]<\/p>\n","protected":false},"author":1470,"featured_media":219126,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-219124","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/219124","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1470"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=219124"}],"version-history":[{"count":3,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/219124\/revisions"}],"predecessor-version":[{"id":219130,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/219124\/revisions\/219130"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/219126"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=219124"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=219124"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=219124"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}