Java中String、StringBuffer和StringBuilder的區別——附示例
已發表: 2022-01-30
在本 Java 教程中,我們將討論 Java String、StringBuffer 和 StringBuilder 之間的區別。
讓我們開始吧:
細繩:
String 類表示字符串。 Java 程序中的所有字符串文字,例如“crunchify”,都是作為此類的實例實現的。 字符串是常量; 它們的值在創建後無法更改。
字符串緩衝區:
一個線程安全的、可變的字符序列。 字符串緩衝區類似於字符串,但可以修改。
在任何時候它都包含一些特定的字符序列,但是序列的長度和內容可以通過某些方法調用來改變。
字符串緩衝區可以安全地被多個線程使用。
字符串生成器:
可變的字符序列。 此類提供與 StringBuffer 兼容的 API,但不保證同步。
此類設計用於在單個線程正在使用字符串緩衝區的地方(通常情況下)用作 StringBuffer 的替代品。
在可能的情況下,建議優先使用此類而不是 StringBuffer,因為它在大多數實現下會更快。
讓我們比較一下 String、StringBuffer 和 StringBuilder 的以下所有特性。
細繩 | 字符串緩衝區 | 字符串生成器 | |
---|---|---|---|
線程安全嗎? | 是的 | 是的 | 不 |
同步? | 是的 | 是的 | 不 |
可修改? | 否(不可變) | 是(可變) | 是(可變) |
貯存 | 字符串池 | 堆 | 堆 |
表現 | 慢 | 快速地 | 快點 |
Java代碼:
- 創建類:CrunchifyStringVsStringBufferVsStringBuilder.java
- 將以下代碼放入其中
- 保存存檔
在本教程中,我們添加、追加字符串總計 199999 次。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
package crunchify . com . tutorial ; /** * @author Crunchify.com * Program: Difference between String, StringBuffer and StringBuilder in Java - Example attached */ public class CrunchifyStringVsStringBufferVsStringBuilder { public static void main ( String [ ] args ) { // String: The String class represents character strings. All string literals // in Java programs, such as "abc", are implemented as instances of this class. // Strings are constant; their values cannot be changed after they are created. String buffers support mutable strings. // Because String objects are immutable they can be shared. String crunchifyString = "" ; long crunchifyBefore = System . currentTimeMillis ( ) ; for ( int i = 0 ; i < 199999 ; i ++ ) { crunchifyString = crunchifyString + "crunchify.com" ; } long crunchifyAfter = ( System . currentTimeMillis ( ) ) ; crunchifyPrint ( "Task Completion Time for String: " + ( crunchifyAfter - crunchifyBefore ) ) ; // StringBuffer: A thread-safe, mutable sequence of characters. A string buffer is like a String, // but can be modified. At any point in time it contains some particular sequence of characters, but the length // and content of the sequence can be changed through certain method calls. // // String buffers are safe for use by multiple threads. The methods are synchronized where necessary so that all // the operations on any particular instance behave as if they occur in some serial order that is consistent with the order // of the method calls made by each of the individual threads involved. // // The principal operations on a StringBuffer are the append and insert methods, // which are overloaded so as to accept data of any type. Each effectively converts a given datum to a string and then appends // or inserts the characters of that string to the string buffer. // The append method always adds these characters at the end of the buffer; // the insert method adds the characters at a specified point. crunchifyBefore = System . currentTimeMillis ( ) ; StringBuffer crunchifyStringBuffer = new StringBuffer ( ) ; for ( int i = 0 ; i < 199999 ; i ++ ) { crunchifyStringBuffer . append ( "crunchify.com" ) ; } crunchifyAfter = ( System . currentTimeMillis ( ) ) ; crunchifyPrint ( "Task Completion Time for StringBuffer: " + ( crunchifyAfter - crunchifyBefore ) ) ; // StringBuilder: A mutable sequence of characters. // This class provides an API compatible with StringBuffer, but with no guarantee of synchronization. // This class is designed for use as a drop-in replacement for StringBuffer in places where the string buffer was being used by a single thread // (as is generally the case). Where possible, // it is recommended that this class be used in preference to StringBuffer as it will be faster under most implementations. // // The principal operations on a StringBuilder are the append and insert methods, // which are overloaded so as to accept data of any type. Each effectively converts a given datum to a string and then appends // or inserts the characters of that string to the string builder. // The append method always adds these characters at the end of the builder; the insert method adds the characters at a specified point. crunchifyBefore = System . currentTimeMillis ( ) ; StringBuilder crunchifyStringBuilder ; crunchifyStringBuilder = new StringBuilder ( ) ; for ( int i = 0 ; i < 199999 ; i ++ ) { crunchifyStringBuilder . append ( "crunchify.com" ) ; } // OR using repeat(). // crunchifyStringBuilder.append("crunchify.com".repeat(199999)); // repeat(): Returns a string whose value is the concatenation of this string repeated count times. crunchifyAfter = ( System . currentTimeMillis ( ) ) ; crunchifyPrint ( "Task Completion Time for StringBuilder: " + ( crunchifyAfter - crunchifyBefore ) ) ; } private static void crunchifyPrint ( String s ) { System . out . println ( s ) ; } } |
IntelliJ IDEA 控制台結果:
在 IntelliJ IDEA 或 Eclipse 控制台中將上述程序作為 Java 應用程序運行,您將得到如下結果。

1 2 3 4 5 6 7 8 |
/ Users / app / app / Installation / jdk - 17.0.2.jdk / Contents / Home / bin / java - javaagent : / Applications / IntelliJ IDEA . app / Contents / lib / idea_rt . jar = 50109 : / Applications / IntelliJ IDEA . app / Contents / bin - Dfile . encoding = UTF - 8 - classpath : / Users / app / . m2 / repository / org / slf4j / log4j - over - slf4j / 1.7.32 / log4j - over - slf4j - 1.7.32.jar crunchify . com . tutorial . CrunchifyStringVsStringBuffer Task Completion Time for String : 19648 Task Completion Time for StringBuffer : 7 Task Completion Time for StringBuilder : 3 Process finished with exit code 0 |
如果您在上面程序運行時遇到任何問題,或者在 String、StringBuffer 和 StringBuilder 之間有任何問題,請告訴我。