Knowledge of the String class

All programs use "" String, String class is an object, even if it is not a new out.

First, the characteristics:

1, you can not change the contents of the string, which is a constant. ( One at a time "," will generate an address, we address only variable is stored, each time with "" assign values to variables, in fact, is to assign a new "" Address to a variable )

2, precisely because the contents of the string can not be changed, so the strings can be shared, that is, if two or more strings have new, its content is the same, then there is only one character in the constant pool .

3, the effect is equivalent string char [] array of characters, in fact, the underlying byte [] array of bytes.

Second, the 3 + 1 common ways to create a string of

Method three configurations:

public String (), create a blank string does not contain any content

public String (char [] array) , according to the character content group to create the corresponding character string.

public String (byte [] array) , in accordance with the byte contents of the array to create the corresponding character string.

A direct creation:

String str = "hello"; // use double quotation marks directly on the right

public class Demo01String {
    public static void main(String[] args) {
        String str1 = new String();
        System.out.println("第一个字符串是:"+str1);

        char[] charArray = {'A','B','C','D'};
        String str2 = new String(charArray);
        System.out.println("第二个字符串是:" + str2);

        byte[] byteArray = {97,98,99};
        String str3 = new String(byteArray);
        System.out.println("第三个字符串是:" + str3);

        String str4 = "hello";
        System.out.println("第四个字符串是:" + str4);

    }
}

Output:

第一个字符串是:
第二个字符串是:ABCD
第三个字符串是:abc
第四个字符串是:hello

 

Published 70 original articles · won praise 4 · Views 3952

Guess you like

Origin blog.csdn.net/l0510402015/article/details/104089676