String, StringBuffer, StringBuilder 차이점
9462 단어 StringBuilder
4
구체적으로 말하면, 당신이 String을 사용한다면
b = b+"aa";
이런 문구일 때.
b
사실 예전의 그 대상은 아니지만,
JVM
메모리 저장소를 다시 구분했다
b+"aa"
그리고
b
이 새 메모리를 가리키고 있습니다.대상
StringBuffer
말하자면
b.append("aa");
집행 후
b
여전히 사용되고 있는 것은 이전의 그 메모리 공간이다.동시에 원래 b가 가리키는 메모리와 메모리 회수에 대한 문제도 있다.
4
public final class String
extends
Object
implements
Serializable ,
Comparable <
String >,
CharSequence
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. For example:
String str = "abc";
is equivalent to:
char data[] = {'a', 'b', 'c'};
String str = new String(data);
Here are some more examples of how strings can be used:
System.out.println("abc");
String cde = "cde";
System.out.println("abc" + cde);
String c = "abc".substring(2,3);
String d = cde.substring(1, 2);
The class
String
includes methods for examining individual characters of the sequence, for comparing strings, for searching strings, for extracting substrings, and for creating a copy of a string with all characters translated to uppercase or to lowercase. Case mapping is based on the Unicode Standard version specified by the Character class. The Java language provides special support for the string concatenation operator (+), and for conversion of other objects to strings.String concatenation is implemented through the
StringBuilder
(or StringBuffer
) class and its append
method. String conversions are implemented through the method toString
, defined by Object
and inherited by all classes in Java. For additional information on string concatenation and conversion, see Gosling, Joy, and Steele,The Java Language Specification . Unless otherwise noted, passing anull argument to a constructor or method in this class will cause a NullPointerException to be thrown.
A
String
represents a string in the UTF-16 format in whichsupplementary characters are represented bysurrogate pairs (see the section Unicode Character Representations in the Character
class for more information). Index values refer to char
code units, so a supplementary character uses two positions in a String
. The
String
class provides methods for dealing with Unicode code points (i.e., characters), in addition to those for dealing with Unicode code units (i.e., char
values). Since:
JDK1.0
public final class StringBuffer
extends
Object
implements
Serializable ,
CharSequence
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. For example, if
z
refers to a string buffer object whose current contents are "start
", then the method call z.append("le")
would cause the string buffer to contain "startle
", whereas z.insert(4, "le")
would alter the string buffer to contain "starlet
". In general, if sb refers to an instance of a
StringBuffer
, then sb.append(x)
has the same effect as sb.insert(sb.length(),x)
. Whenever an operation occurs involving a source sequence (such as appending or inserting from a source sequence) this class synchronizes only on the string buffer performing the operation, not on the source.
Every string buffer has a capacity. As long as the length of the character sequence contained in the string buffer does not exceed the capacity, it is not necessary to allocate a new internal buffer array. If the internal buffer overflows, it is automatically made larger.As of release JDK 5, this class has been supplemented (보충) with an equivalent class 디자인ned for use by a single thread,StringBuilderTheStringBuilder class should generally be used in preference to this one, as it supports all of the same operations but it is faster, as it performs no synchronization.
Since:
JDK1.0
public final class StringBuilder
extends
Object
implements
Serializable ,
CharSequence
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. (faster!) 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. For example, if
z
refers to a string builder object whose current contents are "start
", then the method call z.append("le")
would cause the string builder to contain "startle
", whereas z.insert(4, "le")
would alter the string builder to contain "starlet
". In general, if sb refers to an instance of a
StringBuilder
, then sb.append(x)
has the same effect as sb.insert(sb.length(),x)
. Every string builder has a capacity. As long as the length of the character sequence contained in the string builder does not exceed the capacity, it is not necessary to allocate a new internal buffer. If the internal buffer overflows, it is automatically made larger. Instances of
StringBuilder
are not safe for use by multiple threads. If such synchronization is required then it is recommended that StringBuffer be used. Since:
1.5
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
String, StringBuffer, StringBuilder 어떻게 다른가?이 String 객체는 어떻게 선언을 하냐에 따라 JVM내의 String constant pool에 저장되기도하고, Heap 영역에 저장되기도 한다. 리터럴 방식으로 생성하게 되면 String constant poo...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.