国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Table of Contents
1. StringBuilder
1.StringBuilder's append operation and expansion
2.StringBuilder的subString()方法toString()方法
3.StringBuilder的其它方法
二、StringBuffer
三、 總結(jié)
Home Java JavaBase Review the past and learn the new (2) In-depth understanding of strings in Java

Review the past and learn the new (2) In-depth understanding of strings in Java

Sep 19, 2020 am 09:36 AM
java string

Review the past and learn the new (2) In-depth understanding of strings in Java

Related learning recommendations: java basic tutorial

In the previous article, we conducted an in-depth analysis of the memory of String and its some features. In this article, we will analyze in depth the other two classes related to String, which are StringBuilder and StringBuffer. What is the relationship between these two classes and String? First, let’s take a look at the class diagram below:

Review the past and learn the new (2) In-depth understanding of strings in Java

It can be seen from the figure that both StringBuilder and StringBuffer inherit AbstractStringBuilder, and AbstractStringBuilder and String implement A common interface CharSequence.

We know that a string is composed of a series of characters. The interior of String is implemented based on char array (based on byte array after jdk9), and the array is usually a continuous memory area. When the array is initialized Then you need to specify the size of the array. In the previous article, we already knew that String is immutable because its internal array is declared final. At the same time, String character splicing, insertion, deletion and other operations are all implemented by instantiating new objects. The StringBuilder and StringBuffer we are going to get to know today are more dynamic than String. Next, let us get to know these two categories together.

1. StringBuilder

You can see the following code in StringBuilder’s parent class, AbstractStringBuilder:

abstract class AbstractStringBuilder implements Appendable, CharSequence {    /**
     * The value is used for character storage.
     */
    char[] value;    /**
     * The count is the number of characters used.
     */
    int count;
}復(fù)制代碼

StringBuilder and String are both implemented based on char arrays. The difference is that StringBuilder has no final modification, which means that StringBuilder can be changed dynamically. Next, let’s take a look at the StringBuilder parameterless construction method. The code is as follows:

 /**
     * Constructs a string builder with no characters in it and an
     * initial capacity of 16 characters.
     */
    public StringBuilder() {        super(16);
    }復(fù)制代碼

In this method, the construction method of the parent class is called. Go to AbstractStringBuilder and see that its construction method is as follows:

    /**
     * Creates an AbstractStringBuilder of the specified capacity.
     */
    AbstractStringBuilder(int capacity) {
        value = new char[capacity];
    }復(fù)制代碼

AbstractStringBuilder The constructor internally initializes an array with capacity. In other words, StringBuilder initializes a char[] array with a capacity of 16 by default. In addition to parameterless construction, StringBuilder also provides multiple construction methods. The source code is as follows:

 /**
     * Constructs a string builder with no characters in it and an
     * initial capacity specified by the {@code capacity} argument.
     *
     * @param      capacity  the initial capacity.
     * @throws     NegativeArraySizeException  if the {@code capacity}
     *               argument is less than {@code 0}.
     */
    public StringBuilder(int capacity) {        super(capacity);
    }    /**
     * Constructs a string builder initialized to the contents of the
     * specified string. The initial capacity of the string builder is
     * {@code 16} plus the length of the string argument.
     *
     * @param   str   the initial contents of the buffer.
     */
    public StringBuilder(String str) {        super(str.length() + 16);
        append(str);
    }    /**
     * Constructs a string builder that contains the same characters
     * as the specified {@code CharSequence}. The initial capacity of
     * the string builder is {@code 16} plus the length of the
     * {@code CharSequence} argument.
     *
     * @param      seq   the sequence to copy.
     */
    public StringBuilder(CharSequence seq) {        this(seq.length() + 16);
        append(seq);
    }復(fù)制代碼

The first method of this code initializes a StringBuilder with a specified capacity. The other two constructors can pass in String and CharSequence respectively to initialize StringBuilder. The capacity of these two constructors will be added to the length of the passed string by 16.

1.StringBuilder's append operation and expansion

In the previous article, we already knew that efficient string splicing can be performed through the append method of StringBuilder. How is the append method implemented? Taking append (String) as an example, you can see that the append of StringBuilder calls the append method of the parent class. In fact, not only append, almost all methods of operating strings in the StringBuilder class are implemented through the parent class. The source code of the append method is as follows:

    // StringBuilder
    @Override
    public StringBuilder append(String str) {        super.append(str);        return this;
    }    
  // AbstractStringBuilder
  public AbstractStringBuilder append(String str) {        if (str == null)            return appendNull();        int len = str.length();
        ensureCapacityInternal(count + len);
        str.getChars(0, len, value, count);
        count += len;        return this;
    }復(fù)制代碼

In the first line of the append method, a null check is first performed, and when it is equal to null, the appendNull method is called. The source code is as follows:

private AbstractStringBuilder appendNull() {        int c = count;
        ensureCapacityInternal(c + 4);        final char[] value = this.value;
        value[c++] = 'n';
        value[c++] = 'u';
        value[c++] = 'l';
        value[c++] = 'l';
        count = c;        return this;
    }復(fù)制代碼

appendNull method first calls ensureCapacityInternal to ensure that the string array capacity is recharged. The ensureCapacityInternal method will be analyzed in detail below. Next, you can see that the "null" character is added to the char[] array value.

We mentioned above that the default capacity of StringBuilder's internal array is 16. Therefore, when splicing strings, you need to first ensure that the char[] array has sufficient capacity. Therefore, the ensureCapacityInternal method is called in both the appendNull method and the append method to check whether the char[] array has sufficient capacity. If the capacity is insufficient, the array will be expanded. The ensureCapacityInternal source code is as follows:

private void ensureCapacityInternal(int minimumCapacity) {        // overflow-conscious code
        if (minimumCapacity - value.length > 0)
            expandCapacity(minimumCapacity);
    }復(fù)制代碼

Interpret if here If the length of the spliced ??string is greater than the length of the string array, expandCapacity will be called for expansion.

void expandCapacity(int minimumCapacity) {        int newCapacity = value.length * 2 + 2;        if (newCapacity - minimumCapacity < 0)
            newCapacity = minimumCapacity;        if (newCapacity < 0) {            if (minimumCapacity < 0) // overflow
                throw new OutOfMemoryError();
            newCapacity = Integer.MAX_VALUE;
        }
        value = Arrays.copyOf(value, newCapacity);
    }復(fù)制代碼

The logic of expandCapacity is also very simple. First, multiply the length of the original array by 2 and add 2 to calculate the expanded array length. Next, it is judged that if newCapacity is less than minimumCapacity, the minimumCapacity value is assigned to newCapacity. Because there is more than one place where the expandCapacity method is called, this code is added to ensure safety.

The next sentence of code is very interesting. Is it possible for newCapacity and minimumCapacity to be less than 0? When minimumCapacity is less than 0, an OutOfMemoryError exception is thrown. In fact, it is less than 0 because it is out of bounds. We know that everything stored in the computer is binary, and multiplying by 2 is equivalent to shifting one bit to the left. Taking byte as an example, a byte has 8 bits. The leftmost bit in a signed number is the sign bit. The sign bit for positive numbers is 0 and for negative numbers is 1. Then the size range that a byte can represent is [-128~127], and if a number is greater than 127, it will be out of bounds, that is, the leftmost sign bit will be replaced by 1 in the second bit from the left, and a negative number will appear. . Of course, it's not byte but int, but the principle is the same.

另外在這個(gè)方法的最后一句通過Arrays.copyOf進(jìn)行了一個(gè)數(shù)組拷貝,其實(shí)Arrays.copyOf在上篇文章中就有見到過,在這里不妨來分析一下這個(gè)方法,看源碼:

 public static char[] copyOf(char[] original, int newLength) {        char[] copy = new char[newLength];
        System.arraycopy(original, 0, copy, 0,
                         Math.min(original.length, newLength));        return copy;
    }復(fù)制代碼

咦?copyOf方法中竟然也去實(shí)例化了一個(gè)對(duì)象??!那不會(huì)影響性能嗎?莫慌,看一下這里僅僅是實(shí)例化了一個(gè)newLength長度的空數(shù)組,對(duì)于數(shù)組的初始化其實(shí)僅僅是指針的移動(dòng)而已,浪費(fèi)的性能可謂微乎其微。接著這里通過System.arraycopy的native方法將原數(shù)組復(fù)制到了新的數(shù)組中。

2.StringBuilder的subString()方法toString()方法

StringBuilder中其實(shí)沒有subString方法,subString的實(shí)現(xiàn)是在StringBuilder的父類AbstractStringBuilder中的。它的代碼非常簡單,源碼如下:

public String substring(int start, int end) {        if (start < 0)            throw new StringIndexOutOfBoundsException(start);        if (end > count)            throw new StringIndexOutOfBoundsException(end);        if (start > end)            throw new StringIndexOutOfBoundsException(end - start);        return new String(value, start, end - start);
    }復(fù)制代碼

在進(jìn)行了合法判斷之后,substring直接實(shí)例化了一個(gè)String對(duì)象并返回。這里和String的subString實(shí)現(xiàn)其實(shí)并沒有多大差別。 而StringBuilder的toString方法的實(shí)現(xiàn)其實(shí)更簡單,源碼如下:

 @Override
    public String toString() {        // Create a copy, don&#39;t share the array
        return new String(value, 0, count);
    }復(fù)制代碼

這里直接實(shí)例化了一個(gè)String對(duì)象并將StringBuilder中的value傳入,我們來看下String(value, 0, count)這個(gè)構(gòu)造方法:

    public String(char value[], int offset, int count) {        if (offset < 0) {            throw new StringIndexOutOfBoundsException(offset);
        }        if (count < 0) {            throw new StringIndexOutOfBoundsException(count);
        }        // Note: offset or count might be near -1>>>1.
        if (offset > value.length - count) {            throw new StringIndexOutOfBoundsException(offset + count);
        }        this.value = Arrays.copyOfRange(value, offset, offset+count);
    }復(fù)制代碼

可以看到,在String的這個(gè)構(gòu)造方法中又通過Arrays.copyOfRange方法進(jìn)行了數(shù)組拷貝,Arrays.copyOfRange的源碼如下:

   public static char[] copyOfRange(char[] original, int from, int to) {        int newLength = to - from;        if (newLength < 0)            throw new IllegalArgumentException(from + " > " + to);        char[] copy = new char[newLength];
        System.arraycopy(original, from, copy, 0,
                         Math.min(original.length - from, newLength));        return copy;
    }復(fù)制代碼

Arrays.copyOfRange與Arrays.copyOf類似,內(nèi)部都是重新實(shí)例化了一個(gè)char[]數(shù)組,所以String構(gòu)造方法中的this.value與傳入進(jìn)來的value不是同一個(gè)對(duì)象。意味著StringBuilder在每次調(diào)用toString的時(shí)候生成的String對(duì)象內(nèi)部的char[]數(shù)組并不是同一個(gè)!這里立一個(gè)Falg!

3.StringBuilder的其它方法

StringBuilder除了提供了append方法、subString方法以及toString方法外還提供了還提供了插入(insert)、刪除(delete、deleteCharAt)、替換(replace)、查找(indexOf)以及反轉(zhuǎn)(reverse)等一些列的字符串操作的方法。但由于實(shí)現(xiàn)都非常簡單,這里就不再贅述了。

二、StringBuffer

在第一節(jié)已經(jīng)知道,StringBuilder的方法幾乎都是在它的父類AbstractStringBuilder中實(shí)現(xiàn)的。而StringBuffer同樣繼承了AbstractStringBuilder,這就意味著StringBuffer的功能其實(shí)跟StringBuilder并無太大差別。我們通過StringBuffer幾個(gè)方法來看

     /**
     * A cache of the last value returned by toString. Cleared
     * whenever the StringBuffer is modified.
     */
    private transient char[] toStringCache;    @Override
    public synchronized StringBuffer append(String str) {
        toStringCache = null;        super.append(str);        return this;
    }    /**
     * @throws StringIndexOutOfBoundsException {@inheritDoc}
     * @since      1.2
     */
    @Override
    public synchronized StringBuffer delete(int start, int end) {
        toStringCache = null;        super.delete(start, end);        return this;
    }  /**
     * @throws StringIndexOutOfBoundsException {@inheritDoc}
     * @since      1.2
     */
    @Override
    public synchronized StringBuffer insert(int index, char[] str, int offset,                                            int len)
    {
        toStringCache = null;        super.insert(index, str, offset, len);        return this;
    }@Override
    public synchronized String substring(int start) {        return substring(start, count);
    }    
// ...復(fù)制代碼

可以看到在StringBuffer的方法上都加上了synchronized關(guān)鍵字,也就是說StringBuffer的所有操作都是線程安全的。所以,在多線程操作字符串的情況下應(yīng)該首選StringBuffer。 另外,我們注意到在StringBuffer的方法中比StringBuilder多了一個(gè)toStringCache的成員變量 ,從源碼中看到toStringCache是一個(gè)char[]數(shù)組。它的注釋是這樣描述的:

toString返回的最后一個(gè)值的緩存,當(dāng)StringBuffer被修改的時(shí)候該值都會(huì)被清除。

我們再觀察一下StringBuffer中的方法,發(fā)現(xiàn)只要是操作過操作過StringBuffer中char[]數(shù)組的方法,toStringCache都被置空了!而沒有操作過字符數(shù)組的方法則沒有對(duì)其做置空操作。另外,注釋中還提到了 toString方法,那我們不妨來看一看StringBuffer中的 toString,源碼如下:

   @Override
    public synchronized String toString() {        if (toStringCache == null) {
            toStringCache = Arrays.copyOfRange(value, 0, count);
        }        return new String(toStringCache, true);
    }復(fù)制代碼

這個(gè)方法中首先判斷當(dāng)toStringCache 為null時(shí)會(huì)通過 Arrays.copyOfRange方法對(duì)其進(jìn)行賦值,Arrays.copyOfRange方法上邊已經(jīng)分析過了,他會(huì)重新實(shí)例化一個(gè)char[]數(shù)組,并將原數(shù)組賦值到新數(shù)組中。這樣做有什么影響呢?細(xì)細(xì)思考一下不難發(fā)現(xiàn)在不修改StringBuffer的前提下,多次調(diào)用StringBuffer的toString方法,生成的String對(duì)象都共用了同一個(gè)字符數(shù)組--toStringCache。這里是StringBuffer和StringBuilder的一點(diǎn)區(qū)別。至于StringBuffer中為什么這么做其實(shí)并沒有很明確的原因,可以參考StackOverRun 《Why StringBuffer has a toStringCache while StringBuilder not?》中的一個(gè)回答:

1.因?yàn)镾tringBuffer已經(jīng)保證了線程安全,所以更容易實(shí)現(xiàn)緩存(StringBuilder線程不安全的情況下需要不斷同步toStringCache) 2.可能是歷史原因

三、 總結(jié)

本篇文章到此就結(jié)束了?!渡钊肜斫釰ava中的字符串》通過兩篇文章深入的分析了String、StringBuilder與StringBuffer三個(gè)字符串相關(guān)類。這塊內(nèi)容其實(shí)非常簡單,只要花一點(diǎn)時(shí)間去讀一下源碼就很容易理解。當(dāng)然,如果你沒看過此部分源碼相信這篇文章能夠幫助到你。不管怎樣,相信大家通過閱讀本文還是能有一些收獲。解了這些知識(shí)后可以幫助我們在開發(fā)中對(duì)字符串的選用做出更好的選擇。同時(shí),這塊內(nèi)容也是面試???,相信大家讀完本文去應(yīng)對(duì)面試官的問題也會(huì)綽綽有余。

If you want to know more about programming learning, please pay attention to the php training column!

The above is the detailed content of Review the past and learn the new (2) In-depth understanding of strings in Java. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

PHP Tutorial
1502
276
How to handle transactions in Java with JDBC? How to handle transactions in Java with JDBC? Aug 02, 2025 pm 12:29 PM

To correctly handle JDBC transactions, you must first turn off the automatic commit mode, then perform multiple operations, and finally commit or rollback according to the results; 1. Call conn.setAutoCommit(false) to start the transaction; 2. Execute multiple SQL operations, such as INSERT and UPDATE; 3. Call conn.commit() if all operations are successful, and call conn.rollback() if an exception occurs to ensure data consistency; at the same time, try-with-resources should be used to manage resources, properly handle exceptions and close connections to avoid connection leakage; in addition, it is recommended to use connection pools and set save points to achieve partial rollback, and keep transactions as short as possible to improve performance.

How to work with Calendar in Java? How to work with Calendar in Java? Aug 02, 2025 am 02:38 AM

Use classes in the java.time package to replace the old Date and Calendar classes; 2. Get the current date and time through LocalDate, LocalDateTime and LocalTime; 3. Create a specific date and time using the of() method; 4. Use the plus/minus method to immutably increase and decrease the time; 5. Use ZonedDateTime and ZoneId to process the time zone; 6. Format and parse date strings through DateTimeFormatter; 7. Use Instant to be compatible with the old date types when necessary; date processing in modern Java should give priority to using java.timeAPI, which provides clear, immutable and linear

Comparing Java Frameworks: Spring Boot vs Quarkus vs Micronaut Comparing Java Frameworks: Spring Boot vs Quarkus vs Micronaut Aug 04, 2025 pm 12:48 PM

Pre-formanceTartuptimeMoryusage, Quarkusandmicronautleadduetocompile-Timeprocessingandgraalvsupport, Withquarkusoftenperforminglightbetterine ServerLess scenarios.2.Thyvelopecosyste,

How does garbage collection work in Java? How does garbage collection work in Java? Aug 02, 2025 pm 01:55 PM

Java's garbage collection (GC) is a mechanism that automatically manages memory, which reduces the risk of memory leakage by reclaiming unreachable objects. 1.GC judges the accessibility of the object from the root object (such as stack variables, active threads, static fields, etc.), and unreachable objects are marked as garbage. 2. Based on the mark-clearing algorithm, mark all reachable objects and clear unmarked objects. 3. Adopt a generational collection strategy: the new generation (Eden, S0, S1) frequently executes MinorGC; the elderly performs less but takes longer to perform MajorGC; Metaspace stores class metadata. 4. JVM provides a variety of GC devices: SerialGC is suitable for small applications; ParallelGC improves throughput; CMS reduces

Using HTML `input` Types for User Data Using HTML `input` Types for User Data Aug 03, 2025 am 11:07 AM

Choosing the right HTMLinput type can improve data accuracy, enhance user experience, and improve usability. 1. Select the corresponding input types according to the data type, such as text, email, tel, number and date, which can automatically checksum and adapt to the keyboard; 2. Use HTML5 to add new types such as url, color, range and search, which can provide a more intuitive interaction method; 3. Use placeholder and required attributes to improve the efficiency and accuracy of form filling, but it should be noted that placeholder cannot replace label.

go by example http middleware logging example go by example http middleware logging example Aug 03, 2025 am 11:35 AM

HTTP log middleware in Go can record request methods, paths, client IP and time-consuming. 1. Use http.HandlerFunc to wrap the processor, 2. Record the start time and end time before and after calling next.ServeHTTP, 3. Get the real client IP through r.RemoteAddr and X-Forwarded-For headers, 4. Use log.Printf to output request logs, 5. Apply the middleware to ServeMux to implement global logging. The complete sample code has been verified to run and is suitable for starting a small and medium-sized project. The extension suggestions include capturing status codes, supporting JSON logs and request ID tracking.

Comparing Java Build Tools: Maven vs. Gradle Comparing Java Build Tools: Maven vs. Gradle Aug 03, 2025 pm 01:36 PM

Gradleisthebetterchoiceformostnewprojectsduetoitssuperiorflexibility,performance,andmoderntoolingsupport.1.Gradle’sGroovy/KotlinDSLismoreconciseandexpressivethanMaven’sverboseXML.2.GradleoutperformsMaveninbuildspeedwithincrementalcompilation,buildcac

go by example defer statement explained go by example defer statement explained Aug 02, 2025 am 06:26 AM

defer is used to perform specified operations before the function returns, such as cleaning resources; parameters are evaluated immediately when defer, and the functions are executed in the order of last-in-first-out (LIFO); 1. Multiple defers are executed in reverse order of declarations; 2. Commonly used for secure cleaning such as file closing; 3. The named return value can be modified; 4. It will be executed even if panic occurs, suitable for recovery; 5. Avoid abuse of defer in loops to prevent resource leakage; correct use can improve code security and readability.

See all articles