4.String、StringBuffer、StringBuilder

A, String type

String type is a reference type, but the class is final modification, is the final class can not be subclassed.

Once the initialization string can no longer be changed, since the stored content of char class String [] array is also final modification, so its immutable, and because the char array and private String class does not provide a modified square array values, so String is immutable.

Because String immutable, it can be shared.

Two, String class API

1.compareTo

Compares two strings lexicographically. Based on the comparison value of each unicode character string.

If the two strings are different, or an index at the beginning and then they have different characters, or they have different lengths, or both. If indexing problems are more index characters this.charAt(k)-anotherString.charAt(k). Representative equal equals 0, less than 0 indicates less than the character string parameter dictionary order, that is greater than 0 is greater than the string parameter string lexicographically.

If they do not differ from the index position, the shorter the longer the string in front of the field order re-string.

2.toLowerCase

All the characters in this String to lowercase.

3.toUpperCase

All the characters in this String are converted to uppercase

4.trim

Returns a copy of the string, ignoring leading and trailing whitespace.

5.toCharArray

Convert this string to a new character array

6.split

The given regular expression matching to split this string

7.substring

Returns a new string, he is a subsequence of this sequence of strings

8.concat

The specified string is connected to this end of the string.

9.indexOf

Returns a character string that appears in this first index

10.intern

String s1 = new String("aaa");
String s2 = s1.intern();

If there is a quoted string constant pool of "aaa", and then return to the reference

If the constant pool "aaa" of the reference string does not exist, then it will be added to the constant pool s1, s1 and returns.

Third, the string comparison

1. compare two string objects created by direct assignment

Direct assignment process to create a string object: First, look for references to the existence of the same string value in a string constant pool, if there is a direct return to the reference, if not then create an object and then added to a string constant pool, then returns its reference.

Note: String class overrides the equals and hashCode methods, it equals the value of the string comparison methods are the same.

2. Create a new direct assignment to create and compare

new process created: String s3 = new String("test");, understand, resolve first "test", 1 repeat the process, and then create a string object string equal value.

//        比较两个通过直接赋值创建的字符串对象
        String s1 = "test";
        String s2 = "test";
        System.out.println(s1 == s2);//true
        System.out.println(s1.equals(s2));//true

//        一个通过直接赋值创建的字符串对象和一个用new创建的字符串对象之间的比较
        String s3 = new String("test");
        System.out.println(s1 == s3);//false
        System.out.println(s3.equals(s1));//true

//        两个用new创建的字符串对象之间的比较
        String s4 = new String("test");
        String s5 = new String("test");
        System.out.println(s4 == s5);//false
        System.out.println(s4.equals(s5));//true

//        测试String的intern方法
        String s6 = s5.intern();
        System.out.println(s1 == s6);//true

四、StringBuilder、StringBuffer

StringBuilder is thread-safe. StringBuffer is thread safe. A variable sequence of characters.

The main API is to insert and append

append to additional content, insert to insert content.

Five, String using the + string concatenation principle

Use as an intermediate StringBuilder String object implementation.

        String str1 = "nishizhuma";
        String str2 = "geiniqidaiergeng";
        String str3 = str1 + str2;
        System.out.println(str3);

Achieve above process can be converted to the bottom.

        String str1 = "nishizhuma";
        String str2 = "geiniqidaiergeng";
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(str1);
        stringBuilder.append(str2);
        String str3 = stringBuilder.toString();

Six speed comparison

Compare String + number of characters using string concatenation and StringBuilder append to splice speed

        String s1 = new String("begin");
        long timeBegin = System.currentTimeMillis();
        for(int i = 0; i < 100000; i++) {
            s1 += i;
        }
        long time = System.currentTimeMillis() - timeBegin;
        System.out.println("string对象" + time);

        StringBuilder s2 = new StringBuilder("begin");
        long timeBegin1 = System.currentTimeMillis();
        for(int i = 0; i < 100000; i++) {
            s2.append(i);
        }
        long time1 = System.currentTimeMillis() - timeBegin1;
        System.out.println("stringBuilder对象" + time1);

The results showed that StringBuilder is better than String.

It is recommended to use StringBuilder if there is a large amount of string concatenation to

Seven empty object splicing

StringBuilder will splice a null string splicing empty object. + Stitching Similarly, because the use of StringBuilder append + stitching implemented.

Source code for

    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;
    }

4.String、StringBuffer、StringBuilder

Guess you like

Origin www.cnblogs.com/dearcabbage/p/11227600.html