String, StringBuffer and StringBuilder explained in detail

String

Define the string's
definitions

  • A string is a sequence of characters, for example: "Hello" string, must be enclosed in a pair of double quotes;
  • Not a basic data type, but a reference type (class)
  • String is an immutable type (Immutable)
  • Cannot be modified once created

Construction method

  • public String()
  • public String(String original)
  • public String(char[] value)
  • public String(byte[] bytes)
  • Instance
  • StringConstructorTest.java

Note: The declared string variable must be initialized before it can be used, otherwise the compiler will report "variable not initialized error"; a string connected in java cannot be written in two lines unless it is connected with "+";

**

Come on~~~ Go directly to the code

**
Code GTQ28 has made some notes, please analyze it carefully.

Insert picture description here

Insert picture description here

Insert picture description here

Insert picture description here

Practice

public class test {
    
    

	public static void main(String[] args) {
    
    
		String s1="北京";
		String s2="中国首都";
		
		String s3="北京中国首都";
		
		String s4=s1+"中国首都";
		String s5="北京"+s2;
		
		String s6=s1+s2;
		
		String s7="北京"+"中国首都";

		final String s8="北京";

		String s9=s8+"中国首都";
		
		System.out.println(s3 == s4);
		System.out.println(s3 == s5);
		System.out.println(s3 == s6);
		System.out.println(s4 == s5);
		System.out.println(s5 == s6);
		System.out.println(s6 == s7);
		System.out.println(s3 == s7);
		System.out.println(s3 == s9);

	}

}

答案:
false
false
false
false
false
false
true
true

The above questions can be done well by yourself, and you can tell the reason for each answer, then your understanding of the concept of "once it is created, cannot be modified" is even passed. Then let's continue to learn...

Common method

Take the length of the string

  • length()

Take the character at a certain index position

  • charAt(int index);

Substring

  • substring(4); intercept from the specified position to the end;

  • substring(0,4); Specify the start position and end position, including the previous index, but not the following index;

String concatenation

  • "+": Not only can connect two strings, but also other basic data types, and then these data types will be directly converted to strings; for example: String str = "1+2"; output string str, The result is: 1+2, not 3;

String encoding conversion
Insert picture description here

StringTokenizer tool class

Parsing string tool class StringTokenizer

  • StringTokenizer is a tool class for parsing strings,
    which separates specified strings according to specified separators; example: NewStringDemo.java

Insert picture description here

StringBuffer和StringBuilder

Insert picture description here

Insert picture description here
Insert picture description here
Insert picture description here

The difference between the three

The difference between StringBuffer and StringBuilder

  • The program is running in a single thread, or there is no need to consider the thread synchronization problem, we should give priority to using the StringBuilder class, the execution efficiency is higher; if you want to ensure thread safety, naturally choose StringBuffer.
  • Note: StringBuilder is more efficient than Stringbuffer. It is recommended to use it at work. Is there a security risk if thread is not safe? No, thread safety issues should be left to the program itself to control, rather than to the tool class to complete.

The difference between String and StringBuffer and StringBuilder

  • StringBuffer and StringBuilder, like String, are also used to represent strings.
  • The String class is an immutable class. Any change to the String will trigger the generation of a new String object;
  • StringBuffer is a mutable class, any changes to the string it refers to will not produce new objects.
  • The same is true for the difference between StringBuffer and StringBuilder. Their principles and operations are basically the same. The difference is that StringBuffer supports concurrent operations, is linearly safe, and is suitable for use in multiple threads.
  • StringBuilder does not support concurrent operations, is linearly unsafe, and is not suitable for use in multiple threads.
  • The newly introduced StringBuilder class is not thread-safe, but its performance in a single thread is higher than that of StringBuffer.

The efficiency test code of the three

public static void main(String[] args) {
    
    
		//测试String、StringBufeer和StringBuilder三者的效率
		String s1=new String();
		long starTime = System.currentTimeMillis();
		for(int i=0;i<50000;i++) {
    
    
			s1+=i;
		}
		long endTime = System.currentTimeMillis();
		System.out.println("String总运行时间:"+(endTime-starTime));
		
		
		StringBuffer sf=new StringBuffer();
		starTime = System.currentTimeMillis();
		for(int i=0;i<50000;i++) {
    
    
			sf.append(i);
		}
		endTime = System.currentTimeMillis();
		System.out.println("StringBuffer总运行时间:"+(endTime-starTime));
		
		StringBuilder sd=new StringBuilder();
		starTime = System.currentTimeMillis();
		for(int i=0;i<50000;i++) {
    
    
			sd.append(i);
		}
		endTime = System.currentTimeMillis();
		System.out.println("StringBuilder总运行时间:"+(endTime-starTime));
		
	}

answer:

String total running time: 6328
StringBuffer total running time: 5
StringBuilder total running time: 3

understanding

Insert picture description here

other

Other: String.format("%.2f",num); keep two decimal places

Concluding remarks

Thank you

Guess you like

Origin blog.csdn.net/zhangzhanbin/article/details/111499427