001.String Creation mode

// Create a String object hi named s1 String s1 ="hi"; // create an empty String object named s2 String s2 = new String(); S3 = new String("hi");

Copy the code

002.String Common methods

CharAt (int index) gets the specified character. SubString () intercepts getBytes() string to byte array. ToCharArray () string toCharArray IndexOf() first occurrence subindex lastIndexOf() last occurrence subindex replace(old,new) replace trim() before and after whitespace toLowerCase() toLowerCase toUpperCase() toUpperCaseCopy the code

003. Conversion between strings and byte arrays

// String to byte array String STR ="Java learning";
byte[] arr = str.getBytes();
for(int i=0; i<arr.length; i++){ System.out.print(arr[i]+""); } System.out.println(); String str1= new String(arr); System.out.println(str1);Copy the code

004. = = and equals

  • = =
    • For primitive types, comparisons are values
    • When referencing a type, the comparison is a memory address
  • equals
    • Comparison is whether two objects are equal

String str1="java";
String str2="java";
String str3 = new String("java");
System.out.println("Str1 and STR2 have the same content?"+str1.equals(str2)); //true
System.out.println("Str1 and STR3 have the same content?"+str1.equals(str3)); //true

System.out.println("Str1 and STR2 have the same address?"+(str1==str2));  //true
System.out.println("Str1 and STR3 have the same address?"+(str1==str3));  //false
Copy the code

005. Immutability of strings

String str1 = "java";
str1 = "hello "+str1; //str1 is no longer pointing"java"In memory space, but pointing to"hello java"
System.out.println(str1);       //hello java
Copy the code

006.String is different from StringBuffer and StringBuilder

  • Variability (essential difference)
    • Immutable String String (the object changes as the content changes)
    • Mutable strings StringBuffer and StringBuilder (contents change, objects do not change)
  • Thread safety
    • String is thread-safe
    • Stringbuffers are thread-safe and use synchronized modifications internally
    • StringBuilder is not thread-safe

007. The StringBuilder and StringBuffer

0071. Common methods

StringBuilder STR = new StringBuilder()"Hello");
str.append(",java");
str.append(",").append("Learning together"); System.out.println(str); System.out.println(str.length()); //12 //delete() delete system.out.println (str.delete(7,12)); Java //replace() replace system.out.println (str.replace(7, 12,"All right, all right.")); //substring() intercepts system.out.println (str.substring(9,11)); System.out.println(str.insert(5,"Fifth position")); //reverse() reverse system.out.println (str.reverse()); // Av = av = ajCopy the code

StringBuilder and StringBuffer

  • Thread safety (essential difference) :
    • Stringbuffers are thread-safe (methods modified by synchronized) and inefficient;
    • StringBuilder is thread insecure and efficient;
  • Execution speed
    • StringBuilder>StringBuffer

008. Some of the questions

String a="a";
String b="b"; String c = a+b; D = new String()"ab");
String e = "a"+"b"; // The object in the constant pool String h ="ab"; 
System.out.println(e==h);   //true
System.out.println((a+b).equals(c)); //true
System.out.println(e==c);          //false
System.out.println(c==d);            //false
System.out.println(c.equals(d));     //true
Copy the code

009. A summary

  • To operate on a small amount of data, use String;
  • Single thread manipulation of large amounts of data in the string buffer, suitable for StringBuilder;
  • Multithreaded manipulation of large amounts of data in a StringBuffer, applicable to StringBuffer;