java String class (super detailed!)

I. Introduction

1. String represents the string type, which belongs to 引用数据类型, but does not belong to, the basic data type.

2. 双引号括起来String objects are used casually in java.


For example: "abc", "def", "hello world!", these are 3 String objects.


3. Java stipulates that the string enclosed in double quotes is 不可变, which means that "abc" is immutable from birth to final death and cannot be changed into "abcd" or "ab"

4. In the JDK, strings enclosed in double quotes, such as "abc" and "def" are directly stored in the " " of the " Method Area字符串常量池 ".

5. Why does SUN store strings in a "string constant pool"?

Because strings are used too frequently in actual development. For the sake of execution efficiency, the string is placed in the string constant pool in the method area.

eg1.

public class StringTest01 {
    
    
    public static void main(String[] args) {
    
    
        // 这两行代码表示底层创建了3个字符串对象,都在字符串常量池当中。
        String s1 = "abcdef";
        String s2 = "abcdef" + "xy";

        // 分析:这是使用new的方式创建的字符串对象。这个代码中的"xy"是从哪里来的?
        // 凡是双引号括起来的都在字符串常量池中有一份。
        // new对象的时候一定在堆内存当中开辟空间。
        String s3 = new String("xy");
    }
}

Insert image description here
eg2.

public class StringTest02 {
    
    
    public static void main(String[] args) {
    
    
        String s1 = "hello";
        // "hello"是存储在方法区的字符串常量池当中
        // 所以这个"hello"不会新建。(因为这个对象已经存在了!)
        String s2 = "hello";
        
        // == 双等号比较的是变量中保存的内存地址
        System.out.println(s1 == s2); // true

        String x = new String("xyz");
        String y = new String("xyz");
        
        // == 双等号比较的是变量中保存的内存地址
        System.out.println(x == y); //false
    }
}

Insert image description here

Note:
"==" cannot be used for comparison between string objects, "==" is not safe. The equals method of String class should be called .

eg3.

String k = new String("testString");
//String k = null;
// "testString"这个字符串可以后面加"."呢?
// 因为"testString"是一个String字符串对象。只要是对象都能调用方法。
System.out.println("testString".equals(k)); // 建议使用这种方式,因为这个可以避免空指针异常。
System.out.println(k.equals("testString")); // 存在空指针异常的风险。不建议这样写。

2. Construction method

Constructor name eg.
String s = “xxx” Most used
String(String original) String(“xxx”)
String(char array)
String (char array, starting subscript, length)
String (byte array)
String (byte array, starting subscript, length)
String(StringBuffer buffer)
String(StringBuilder builder)

eg.

class StringTest{
    
    
    public static void main(String[] args) {
    
    
    	//最常用的方式
        String s1 = "我是中国人";
        System.out.println(s1);//我是中果人

        String s2 = new String("我是中国人");
        System.out.println(s2);//我是中果人

        char[] c = {
    
    '我' , '是', '中', '果', '人'};
        String s3 = new String(c);
        System.out.println(s3);//我是中果人

        String s4 = new String(c, 2, 3);
        System.out.println(s4);//中果人

        byte[] b = {
    
    65, 66 ,67, 68};
        String s5 = new String(b);
        System.out.println(s5);//ABCD

        String s6 = new String(b, 1, 2);
        System.out.println(s6);//BC

        StringBuffer sb1 = new StringBuffer("我是福建人");
        String s7 = new String(sb1);
        System.out.println(s7);//我是福建人

        StringBuilder sb2 = new StringBuilder("我是厦门人");
        String s8 = new String(sb2);
        System.out.println(s8);//我是厦门人
    }
}

3. Method

method name effect
char charAt (int index) Returns the character at the specified position
int compareTo(String anotherString) Compares two strings. Returns 0 if equal; returns 1 if the front is larger than the bottom; returns -1 if the front is small and the bottom is large.
boolean contains(CharSequence s) Determine whether the string contains s
boolean endsWith(String suffix) Determine whether the string ends with suffix
boolean equals(Object anObject) Determine whether two strings are equal
boolean equalsIgnoreCase(String anotherString) Determine whether two strings are equal, ignoring case
byte[] getBytes() Return the string into a byte array
int indexOf(String str) Returns the position of the first occurrence of str in the string
boolean isEmpty() Whether the string is empty
int length() string length
int lastIndexOf(String str) Returns the position of the last occurrence of str
String replace(CharSequence target, CharSequence replacement) Replace the characters of the string target with replacement
String[] split(String regex) Split string by regex
boolean startsWith(String prefix) Determine whether the string starts with prefix
String substring(int beginIndex) Intercept the string starting from beginIndex
String substring(int beginIndex, int endIndex) Intercept the string from beginIndex to endIndex - 1
char[] toCharArray() Convert string to char array
String toLowerCase() Convert string to lowercase
String toUpperCase() Convert string to uppercase
String trim() Remove spaces from both sides of string
static method
static String valueOf(int i) Convert i to string

eg.

class StringTest{
    
    
    public static void main(String[] args) {
    
    
        String s1 = "hello world";

        System.out.println(s1.charAt(6));//w

        String s2 = "abc";
        String s3 = "xyz";
        String s4 = "xyz";
        System.out.println(s2.compareTo(s3));//-23
        System.out.println(s3.compareTo(s4));//0
        System.out.println(s4.compareTo(s1));//16

        System.out.println(s2.equals(s3));//false

        System.out.println(s1.endsWith("world"));//true
        System.out.println(s1.endsWith("t"));//false

        String s5 = "HELLO worLD";
        System.out.println(s1.equalsIgnoreCase(s5));//true

        byte[] b = s1.getBytes();
        System.out.println(Arrays.toString(b));//[104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100]

        System.out.println(s1.indexOf("world"));//6
        System.out.println(s1.indexOf("h"));//0

        System.out.println(s1.isEmpty());//false

        System.out.println(s1.length());//11

        String s6 = "javapythonc++cphpjavapython";
        System.out.println(s6.lastIndexOf("java"));//17
        System.out.println(s6.lastIndexOf("h"));//24

        String s7 = "name=zhangsan&age=18&sex=男";
        String newS7 = s7.replace("&", ";");
        System.out.println(newS7);//name=zhangsan;age=18;sex=男

        String[] splitS7 = s7.split("&");
        System.out.println(Arrays.toString(splitS7));//[name=zhangsan, age=18, sex=男]

        System.out.println(s6.startsWith("java"));//true
        System.out.println(s6.startsWith("python"));//false

        System.out.println(s6.substring(10));//c++cphpjavapython
        System.out.println(s6.substring(10, 13));//c++

        char[] charS6 = s6.toCharArray();
        System.out.println(Arrays.toString(charS6));//[j, a, v, a, p, y, t, h, o, n, c, +, +, c, p, h, p, j, a, v, a, p, y, t, h, o, n]

        System.out.println(s6.toUpperCase());//JAVAPYTHONC++CPHPJAVAPYTHON

        System.out.println(s5.toLowerCase());//hello world

        String s8 = "           你 好 世 界                   ";
        System.out.println(s8.trim());//你 好 世 界

        System.out.println("------------------------------");

        System.out.println(String.valueOf(123));//123
        System.out.println(String.valueOf(3.14));//3.14
        System.out.println(String.valueOf(true));//true
        System.out.println(String.valueOf(new Object()));//java.lang.Object@4554617c
        //valueOf会自动调用toString()方法

    }
}

Note:
1. Why does System.out.println (reference) automatically call the toString() method?

Because println() will call the String.valueOf() method and String.valueOf() will call the toString() method

2.String byte array understanding

byte[] b = new byte[1000000000];//1000000000 are all of byte type

Guess you like

Origin blog.csdn.net/qq_44715943/article/details/116308837