编程知识 cdmana.com

Java learning_ Java core class

  • String and encoding
    • The string is in String Inside is through a char[] The array represents , therefore , It can be written as follows :
      String s2 = new String(new char[] {'H', 'e', 'l', 'l', 'o', '!'});

    • Java An important feature of string is that the string is immutable . This immutability is through internal private final char[] Field , And without any changes char[] The method of .
    • Java Compiler at compile time , Will automatically put all the same string as an object into the constant pool .
    • Comparison of two strings , Must always use equals() Method .
    • Ignore case comparison , Use equalsIgnoreCase() Method .
    • String It also provides isEmpty() and isBlank() To determine whether the string is empty and empty string .
    • Concatenate strings using static methods join(), It concatenates the string array with the specified string .
      String[] arr = {"A", "B", "C"};String s = String.join("***", arr); // "A***B***C"

    • The string provides formatted() Methods and format() Static methods , You can pass in other parameters , Replace placeholders , And then generate a new string .
      1 public class Main {2  public static void main(String[] args) {3   String s = "Hi %s, your score is %d!";4   System.out.println(s.formatted("Alice", 80));5   System.out.println(String.format("Hi %s, your score is %.2f!", "Bob", 59.5));6  }7 }

    • To convert any basic or reference type to a string , You can use static methods valueOf().
      1 String.valueOf(123); // "123"2 String.valueOf(45.67); // "45.67"3 String.valueOf(true); // "true"4 String.valueOf(new Object()); //  similar java.lang.Object@636be97c

    • new String(char[]) Create a new String When an instance , It doesn't directly refer to the incoming char[] Array , It's a copy , therefore , Modify the external char[] Arrays do not affect String Inside the instance char[] Array , Because these are two different arrays .
    • Well, we often use UTF-8 What's the code ? Because of the English characters Unicode Encoding high bytes is always 00, A lot of English text is a waste of space , therefore , There is UTF-8 code , It's a variable length code , Used to fix the length of Unicode Code to 1~4 Variable length encoding of bytes . adopt UTF-8 code , English characters 'A' Of UTF-8 The encoding changes to 0x41, Coincides with ASCII The codes are the same , And Chinese ' in ' Of UTF-8 Encoded as 3 byte 0xe4b8ad.
  • StringBuilder

    • String Although you can concatenate strings directly , however , In circulation , Each loop creates a new string object , Then throw away the old string .
    • In order to efficiently concatenate strings ,Java The standard library provides StringBuilder, It's a mutable object , Buffer can be preallocated , such , Go to StringBuilder When adding a new character in , No new temporary objects are created .
    • StringBuilder You can also do chain operations .
      1 public class Main { 2  public static void main(String[] args) { 3   var sb = new StringBuilder(1024); 4   sb.append("Mr ") 5   .append("Bob") 6   .append("!") 7   .insert(0, "Hello, "); 8   System.out.println(sb.toString()); 9  }10 }

    • see StringBuilder Source code , You can find , The key to chain operation is , Defined append() Method will return this, such , You can call your own other methods .
    • For ordinary strings + operation , We don't need to rewrite it as StringBuilder, because Java The compiler automatically compiles multiple consecutive + The operation code is StringConcatFactory The operation of . In operation period ,StringConcatFactory Will automatically optimize string join operation to array copy or Stri.........

版权声明
本文为[Program ape Owen]所创,转载请带上原文链接,感谢
https://cdmana.com/2020/12/20201225010838354v.html

Scroll to Top