【JavaSE】String class

The difference between two ways to create String objects

String s1 = "hello";
String s2 = new String("hello");

s1 first checks whether the constant pool has a "hello" data space. If there is, it points directly to it. If not, it creates it and points to it. S1 ultimately points to the space address of the constant pool.
s2 first creates a space in the heap, which has a value attribute, pointing to the "hello" space of the constant pool. If there is no "hello" in the constant pool, it will be created. If it does, it will be pointed to by value. s2 ultimately points to the space address in the heap. .
Memory layout of s1 and s2
Insert image description here
Then let’s look at the following questions:

String a = "abc";
String b = "abc";
// true equals比较的是值是否相等
System.out.println(a.equals(b)); 
// true a和b的地址是一样的
System.out.println(a==b); 
String a = "hello";
String b = new String("hello");
// true  比较的是值
System.out.println(a.equals(b));
 // false  根据上面的内存布局可以看出a,b地址不一样
System.out.println(a==b);
//b.intern() 返回的是常量池的地址 所以是 true
System.out.println(a==b.intern()); 
//b指向堆中的地址,b.intern()返回的是常量池的地址所以是false
System.out.println(b==b.intern()); 
String s1 = "hello java";
String s2 = "hello";
String s3 = "hello";
String s4 = new String("hello");
System.out.println(s2==s4); //false 地址不一样
System.out.println(s2==s3); //true 都指向常量池的hello空间
System.out.println(s2.equals(s3));// true 比较内容是否相等
System.out.println(s1==s2); // false 指向的常量池地址不一样
Person p1 = new Person();
p1.name = "小徐";
Person p2 = new Person();
p2.name = "小徐";
// 如图所示 地址相同 true
System.out.println(p1.name==p2.name);
//比较的是值是否相等 true
System.out.println(p1.name.equals(p2.name)); 
//true
System.out.println(p1.name=="小徐");

Insert image description here
The String class is a final class that represents an immutable sequence of characters. Strings are immutable. Once a string object is allocated, its value is immutable.

String s = "java";
s = "hello";

The above code creates a total of two objects
Insert image description here

String s1 = "java";
String s2 = "hello";
//根据debug我们可以知道,先会创建一个StringBuilder对象,
//然后后执行里面的append方法,最后调用toString
String s3 = s1 + s2;

Insert image description here

public class StringExcise02 {
    
    
    String str = new String("hello");
    final char[] ch = {
    
    'j','a','v','a'};
    public void change(String str,char[] ch) {
    
    
        str = "java";
        ch[0] = 'x';
    }
    public static void main(String[] args) {
    
    
        StringExcise02 ex= new StringExcise02();
        ex.change(ex.str, ex.ch);
        System.out.println(ex.str + "end"); //helloend
        System.out.println(ex.ch); //xava
    }
}

Insert image description here
StringBuffer class
StringBuffer stores string variables, and the values ​​inside can be changed. There is no need to create a new object for each update, and the efficiency is higher than String.
String to StringBuffer

String str = "java";
//返回的stringBuffer才是StringBuffer对象,对str本身没有影响
StringBuffer stringBuffer = new StringBuffer(str);
//使用append方法
StringBuffer stringBuffer1 = new StringBuffer();
stringBuffer1 = stringBuffer1.append("java");

StringBuffer to String

StringBuffer stringBuffer2 = new StringBuffer("java");
//使用StringBuffer提供的toString方法
String s = stringBuffer2.toString();
//使用构造器s
String s1 = new String(stringBuffer2);

Example: Print the number 234156.33 according to the example 234,156.33

public class StringExcise04 {
    
    
    public static void main(String[] args) {
    
    
        String price = "234156.33";
        StringBuffer stringBuffer = new StringBuffer(price);
        for (int i = stringBuffer.lastIndexOf(".") - 3; i > 0; i -= 3) {
    
    
            stringBuffer = stringBuffer.insert(i, ",");
        }
        System.out.println(stringBuffer);
    }
}

StringBuilder class
The biggest difference between String and StringBuilder is that the content of String cannot be modified, while the content of StringBuilder can be modified. Consider using StringBuilder if strings are frequently modified.

The String and StringBuilder classes cannot be converted directly. If you want to convert each other, you can adopt the following principles:
String becomes StringBuilder: Use the constructor or append() method of StringBuilder.
StringBuilder becomes String: Call the toString() method.

The difference between String, StringBuffer and StringBuilder.

  • The contents of String cannot be modified, but the contents of StringBuffer and StringBuilder can be modified.
  • Most functions of StringBuffer and StringBuilder are similar
  • StringBuffer uses synchronous processing and is a thread-safe operation; StringBuilder does not use synchronous processing and is a thread-unsafe operation.

Guess you like

Origin blog.csdn.net/qq_58032742/article/details/132629373