Java common class – string class

String related classes: String

String class: represents a string. All string literals (such as "ABC") in Java programs are implemented as instances of this class.

public void test1(){
        String s1 = "abc";//字面量的方式
        String s2 = "abc";

        System.out.println(s1 == s2);// ture,比较的是s1和s2的地址值
        s1 = "hello";

        System.out.println(s1); // hello
        System.out.println(s2); // abc

        String s3 = "abc";
        s3 += "def";
        System.out.println(s3); // abcdef
        System.out.println(s2 == s3); // false,此时的s2和s3的地址值已经不同


        String s4 = "abc";
        String s5 = s4.replace('a','d');
        System.out.println(s4); // abc
        System.out.println(s5); // dbc
    }

1. Instantiation method of string

  public void test2(){

        String s1 = "Java";
        String s2 = "Java";

        String s3 = new String("Java");
        String s4 = new String("Java");

        System.out.println(s1 == s2); // true
        System.out.println(s1 == s3); // false
        System.out.println(s1 == s4); // false
        System.out.println(s3 == s4); // false



        Person p1 = new Person("Tom",18);
        Person p2 = new Person("Tom",18);
        System.out.println(p1.name.equals(p2.name)); // true,String重写了equals方法,此时比较的就是内容了
        System.out.println(p1.name == p2.name); // true,两个对象里面还是字面量定义的方式赋值
    }

Note: the two objects are assigned by literal definition

Interview questions

String s = new string ("ABC") method to create objects. How many objects are created in memory?

Two, one is the new structure of heap space, and the other is the data "ABC" in the constant pool corresponding to char []

2. Character string

a key:

public void test3(){
        String s1 = "Java";
        String s2 = "Python";

        String s3 = "JavaPython";
        String s4 = "Java" + "Python";
        String s5 = s1 + "Python";
        String s6 = s1 + s2;

        System.out.println(s3 == s4); // true

        System.out.println(s3 == s5); // false
        System.out.println(s4 == s5); // false
        System.out.println(s3 == s6); // false

        String s7 = s5.intern();
        System.out.println(s7 == s3); //true
    
    }

Special note: when final string S1 = "Java", this is equivalent to a constant

3. Interview questions:

good and best

Here you can refer to the previous Java value passing

What happens if you add this before SCR?

package com.atguigui.exer;

/**
 * @author MD
 * @create 2020-07-12 9:41
 */
public class StringTest {

    String str = new String("good");
    char[] ch = {'t','e','s','t'};

    public void change(String str,char ch[]){
        this.str = "test ok";
        ch[0] = 'b';
    }

    public static void main(String[] args) {
        StringTest ex = new Stringtest();
        ex.change(ex.str,ex.ch);
        System.out.println(ex.str);
        System.out.println(ex.ch);
        // test ok
        // best
        
    }
}

4. String common methods

Note the immutability of string, and the original character remains unchanged

5. String and basic data type conversion

String -- > basic data type, wrapper class

Basic data type, wrapper class -- > string

public void test2(){
        String str = "123";
        
        int num = Integer.parseInt(str);
        
        String str1 = String.valueOf(num);
    }

String -- > char []: call string's tochararray()

Char [] --- > string: call the constructor of string

public void test3(){
        String str = "asdf123";

        char[] charArray = str.tocharArray();
        for (int i = 0; i < charArray.length; i++) {
            System.out.println(charArray[i]);
        }

        String str1 = new String(charArray);
        System.out.println(str1);

    }

Encoding: String -- > byte []: call getbytes() of string decoding: byte [] --- > string: call the constructor of string

/**
     * 编码:字符串 -->字节 (看得懂的---->看不懂的二进制数据)
     * 解码:字节 ---> 字符串(看不懂的二进制数据---->看得懂的)
     * @throws UnsupportedEncodingException
     */
    @Test
    public void test4() throws UnsupportedEncodingException {
        String str = "abc123你好";
        byte[] bytes = str.getBytes(); // 使用默认的字符集进行转换 UTF-8 编码
        System.out.println(Arrays.toString(bytes));

        String str1 = "abc123你好";
        byte[] bytes1 = str1.getBytes("gbk"); // 指定编码格式
        System.out.println(Arrays.toString(bytes1));


        System.out.println("---------------------");
        String str2 = new String(bytes); // 解码,使用的默认的格式
        System.out.println(str2);

        String str3 = new String(bytes1,"gbk");// 解码,使用的指定的格式
        System.out.println(str3);
    }

6. StringBuffer、StringBuilder ?

Interview question: compare string, StringBuffer and StringBuilder?

Note: if passed as a parameter, the internal string of the method will not change its value, and StringBuffer and StringBuilder will change its value

源码分析:
String str = new String(); // char[] value = new char[0];
String str1 = new String("abc"); // char[] value = new char[]{'a','b','c'};

StringBuffer sb1 = new StringBuffer(); // char[] value = new char[16];底层创建一个长度16的char型数组
StringBuffer sb1 = new StringBuffer("abc"); // char[] value = new char["abc".length()+16];

Question 1:

Note that this is the length of the data in it,

public void test5(){
        StringBuffer sb1 = new StringBuffer();
        System.out.println(sb1.length()); // 0

        StringBuilder sb2 = new StringBuilder("abc");
        System.out.println(sb2.length()); // 3

    }

Question 2:

For capacity expansion, if the data to be added cannot be put, the array needs to be expanded. By default, the expansion is twice the original + 2, and the data in the original array is copied to the new array

String ----- > StringBuffer, StringBuilder: just call the StringBuffer and StringBuilder constructors

StringBuffer, StringBuilder ----- >: call the constructor of string

The common methods of the StringBuffer class are similar to those of StringBuilder

Summary:

Interview questions:

 public void testStringBuffer(){
        String str = null;
        StringBuffer sb = new StringBuffer();
        sb.append(str);

        System.out.println(sb.length()); // 4

        System.out.println(sb); //  "null" 这个是字符串null

        StringBuffer sb1 = new StringBuffer(str); // 抛异常 NullPointerException
        System.out.println(sb1);
    }
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>