The String class knowledge list of common Java classes is simple and easy to understand

Basic knowledge of String class

1. The String class represents a String and is understood as an immutable character sequence

2. The String class is declared as final and cannot be inherited

3. The String class implements the Serializable interface: it indicates that the String supports serialization (serialization: the java object can be transformed into a binary file to transfer the java object)

4. The String class implements the Comparable interface: it means that strings can be compared in size

5. The bottom layer is stored with final char [] value

Creation method of String class object

1. Literal mode

String s1="abcd"

This method first declares a String type variable s1 in the stack space, then stores abcd in the constant pool in the method area, assigns the storage address value (Ox0001) to s1, and makes s1 point to abcd

 

2. Constructor mode

String s2=new String("abcd");

This method first declares a String type variable s2 in the stack space, then opens up a space in the heap space, assigns the address value of the space (Ox1111) to s2, and makes s2 point to it. Store abcd in the constant pool in the method area, and assign the storage address value (Ox0001) to the value attribute to point to abcd

The address values shown in the figure are assumed address values

Other classes create objects only in constructor mode

Concatenation of strings

1,str="aaa"+"bbb"

String constants and string constants still exist in the constant pool after being connected, and there is only one copy of the same data in the constant pool.

Therefore, if the connected data (aaabbb) already exists in the constant pool, the address is assigned to the variable (str) that receives it

If it does not exist, a space will be opened in the constant pool to store the connected data (aaabbb), and the newly opened address will be assigned to the variable receiving it (str)

2,str=s1+"bbb"

After the string variable is connected with the string constant, first create a new object in the heap space, assign the address of the object to the variable (str) that receives it, and then point the object to the constant pool. Therefore, whether the connected data exists in the constant pool or not, the connected variable (str) points to the address of the object in the heap space

2,str=s1+s2

The connection of string variable and string variable is the same as 2.

Code example:

public class StringTest {
    public static void main(String[] args) {
        String s1="aaa";
        String s2="bbb";
        String s3="aaabbb";
        String s4="aaa"+"bbb";
        System.out.println(s3==s4);//true, there is only one copy of the same data in the constant pool, so s4 is assigned the address of s3
        String s5=s1+"bbb";
        System.out.println(s3==s5);//false, as long as one is a variable, it is equivalent to a new object in the heap space, which points to the constant pool. Therefore, s5 is assigned the address of the new object in the heap space
        String s6=s1+s2;
        System.out.println(s5==s6);//false, both are variables, which is equivalent to a new object in the heap space, which points to the constant pool. Therefore, s6 is assigned the address of the new object in the heap space
        System.out.println(s3==s6.intern());//True, the intern () method returns the storage address in the constant pool
    }
}

 

Examples of common methods of String class:

public class StringTest2 {
    public static void main(String[] args) {
        String str="Hello World";
        System.out.println("The length of the string is:"+str.length());//Include spaces
        System.out.println("Characters with subscript 1 are:"+str.charAt(1));
        System.out.println("Whether the string is empty:"+str.isEmpty());
        System.out.println("Convert all to lowercase:"+str.toLowerCase());
        System.out.println("Convert all to uppercase:"+str.toUpperCase());
        str="     Hello   World    ";
        System.out.println("Remove the leading and trailing spaces:"+str.trim());
        String s1="Hello World";
        String s2=new String("Hello World");
        System.out.println("s1 and s2 Whether the address values of are the same:"+(s1==s2));//==Compare address values
        System.out.println("s1 and s2 Are the contents of the same:"+s1.equals(s2));//equals compare content
    }
}

 

Equalsignorecase (object anobject): after ignoring case, compare whether the contents of the string are the same

concat(String str): concatenate strings

CompareTo (string another string): compare string sizes

substring(int beginIndex): intercepts the substring from the specified index

startsWith(String prefix): whether to start with the specified substring

endsWith(String suffix): whether to end with the specified substring

contains(String str): whether to include substrings

indexOf(int ch): returns the index of the first occurrence of the substring. If there is no occurrence, return - 1

lastIndexOf(int ch): returns the index of the last occurrence of the substring. If there is no occurrence, return - 1

replace(char oldChar,char newChar): replace old characters with new ones

matches(String regex): match by regular expression

split(String regex): splits a string into a string array according to a regular expression

Convert String type to int type

Integer.parseInt(str)

Convert int type to String type

String.valueOf(i) or "i +"

Keywords: Java

Added by Robert Plank on Thu, 03 Feb 2022 07:18:40 +0200