String, StringBuffer, StringBuilder 차이점

9462 단어 StringBuilder
4
  • String은 비가변 클래스로 그 대상은 문자열 상수이기 때문에 빈번하게 변경하기에 적합하지 않다. 예를 들어 삽입(insert), 삭제(delete), 추가(append) 등이다

  • 4
  • StringBuffer는 가변 클래스로 그 대상은 수정 가능한 문자 서열로 빈번한 수정 문자열에 적합하다. 예를 들어 자주 사용하는 것은 추가(append)이다.게다가 라인이 안전합니다.여러 번 수정이 필요할 때 가능한 이 종류를 사용하세요

  • 구체적으로 말하면, 당신이 String을 사용한다면
    b = b+"aa";
    이런 문구일 때.
    b
    사실 예전의 그 대상은 아니지만,
    JVM
    메모리 저장소를 다시 구분했다
    b+"aa"
    그리고
    b
    이 새 메모리를 가리키고 있습니다.대상
    StringBuffer
    말하자면
    b.append("aa");
    집행 후
    b
    여전히 사용되고 있는 것은 이전의 그 메모리 공간이다.동시에 원래 b가 가리키는 메모리와 메모리 회수에 대한 문제도 있다.
    4
  • StringBuilder는 가변 클래스이지만 단일 라인을 바탕으로 한다. 즉, 다중 라인은 안전하지 않고 나머지는StringBuffer와 같다.그래서 속도가 상대적으로 빠르기 때문에 다선정과 관련이 없다면 이것을 사용하는 것을 권장합니다
  • 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

    좋은 웹페이지 즐겨찾기