JAVA---Detailed Explanation of String and StringBuilder

1. String construction method

  •  public String():创建一个空白字符串对象。不含有任何内容
    
  •  public String(char[] chs):根据字符数组的内容,来创建字符串对象
    
  •  public String(byte[] bys):根据字节数组的内容,来创建字符串对象
    
  •  String s="abc":直接赋值的方式创建字符串对象,内容是abc
    

The most commonly used is the last one, direct assignment

It should be noted that :
for the first construction method, their content is the same, but the address is different

		char[] chs= {
    
    'a','b','c'};
		String s1=new String(chs);
		String s2=new String(chs);
		//比较地址是否相同
		System.out.println(s1==s2);//返回false
		//比较字符串内容是否相同
		System.out.println(s1.equals(s2));//返回true

For the direct assignment method: their content and address are the same

		String s3="abc";
		String s4="abc";
		//比较地址是否相同
		System.out.println(s3==s4);//返回true
		//比较字符串内容是否相同
		System.out.println(s3.equals(s4));返回true

The picture below can explain in detail the memory allocation when String is created.
Insert picture description here


2. Use String for string splicing

After the String is created, its value is not allowed to be modified, so every time a String object is spliced, a new String object will be re-created. This operation is time-consuming and wastes memory space.

Insert picture description hereSo you can use the StringBuilder class provided by JAVA to solve this problem


Three. StringBuilder construction method

  •  public StringBuilder()
    
  • Create a blank variable string object without any content
  •  public StringBuilder(String str)
    
  • According to the content of the string, to create a variable string object

Four. StringBuilder adding and reversing methods

public StringBuilder append(任意类型)
//添加数据,并返回本身
public StringBuilder reverse()
//返回相反的字符序列
public class stringbuilderDemo {
    
    
	public static void main(String[] args) {
    
    
		//public StringBuilder():创建一个空白可变的字符串对象,不含有任何内容
		StringBuilder s1=new StringBuilder();
		System.out.println("s1:"+s1);
		//输出s1:
		System.out.println("s1.length:"+s1.length());
		//输出s1.length:0
		
		//public StringBuilder(String str):根据字符串的内容,来创建可变字符串对象
		StringBuilder s2=new StringBuilder("helloworld");
		System.out.println("s2:"+s2);
		//输出s2:helloworld
		System.out.println("s2.length:"+s2.length());
		//输出s2.length:10
		
		//public StringBuilder append(任意类型):添加数据,并返回对象本身
		StringBuilder s3=new StringBuilder();
		StringBuilder s4=s3.append("hello");
		
		System.out.println("s3:"+s3);
		//输出s3:hello
		System.out.println("s4:"+s4);
		//输出s4:hello
		System.out.println(s3==s4);
		//输出true
		
		s3.append("world").append("100");
		System.out.println(s3);
		//输出helloworld100
		
		s3.reverse();
		System.out.println(s3);
		//输出001dlrowolleh
		}
	}


Five. Conversion between StringBuilder and String

  • (1).StringBuilder->String
  •  public String toString()
    
  • Convert StringBuilder to String through toString method
  • (2).String->StringBuilder
  •  public StringBuilder(String s)
    
  • Through the construction method, String can be converted into StringBuilder
public class stringbuilderDemo {
    
    
	public static void main(String[] args) {
    
    
	
	StringBuilder s1=new StringBuilder();
		s1.append("hello");
		
		String s2=s1.toString();
		System.out.println(s2);		//输出hello
		
		String s3="helloworld";
		
		StringBuilder s4=new StringBuilder(s3);
		System.out.println(s4);		//输出helloworld
	}
}

Guess you like

Origin blog.csdn.net/weixin_45102820/article/details/112982018