当前位置:网站首页>Let's talk about string today

Let's talk about string today

2022-06-25 23:51:00 Program diary

String

How to implement and feature

String Class quilt final modification , therefore String Not to be inherited ,Integer And so on can not be inherited .

jdk1.8 in ,String Bottom use final Embellished char Array to store strings

private final char value[];

jdk1.9 after , Bottom use final Embellished byte Array to store strings

private final byte[] value;

Use final Keyword modification , Illustrate this value After the array is initialized, you cannot reference other arrays ,String There's no change inside value Array method , therefore String immutable .

Immutable benefits

  1. Cache hash value

    for example HashMap Use in String As key, Because it's immutable , So the hash value only needs to be calculated once ,String There is also relevant code in the class .

    /** Cache the hash code for the string */
        private int hash; // Default to 0
    
  2. String Pool( String constant pool ) The need for

    When one String The object has been created , Will be from String Pool Get a reference to , If String Variable words ,String Pool Can't use .

  3. Thread safety

String,StringBuffer,StringBuilder

  1. variability

    • String immutable

    • StringBuffer and StringBuilder variable

      Why do these two change , Because they all inherit from one AbstractStringBuilder, This class provides constructors , Based on a variable capacity array

      /** * Creates an AbstractStringBuilder of the specified capacity. */
      AbstractStringBuilder(int capacity) {
              
          value = new char[capacity];
      }
      

      Both of them call the parent class constructor , Pass in a default capacity 16 As the initial capacity

      // StringBuilder
      public StringBuilder(String str) {
              
              super(str.length() + 16);
              append(str);
          }
      // StringBuffer
      public StringBuffer(String str) {
              
              super(str.length() + 16);
              append(str);
          }
      

      append Method , Capacity controllable

      public AbstractStringBuilder append(String str) {
              
              if (str == null)
                  return appendNull();
              int len = str.length();
              ensureCapacityInternal(count + len);
              str.getChars(0, len, value, count);
              count += len;
              return this;
          }
      private void ensureCapacityInternal(int minimumCapacity) {
              
              // overflow-conscious code
              if (minimumCapacity - value.length > 0) {
              
                  value = Arrays.copyOf(value,
                          newCapacity(minimumCapacity));
              }
          }
      
  2. Thread safety

    • String Thread safety

    • StringBuffer Thread safety , Internal method locking , such as append

      @Override
      public synchronized StringBuffer append(String str) {
              
          toStringCache = null;
          super.append(str);
          return this;
      }
      
    • StringBuilder Thread unsafe , Because there is no lock , Performance is better than StringBuffer A little better

  3. Use scenarios

    • String Operate on a small amount of data
    • StringBuffer Multithreading , Working with a lot of data
    • StringBuilder Single thread , Working with a lot of data

StringPool

intern Method

have access to String Of intern() Method to add a string to String Pool in .

When a string is called intern() When the method is used , If String Pool A string already exists that is equal to the value of the string ( Use equals() Method to determine ), So it's going to return String Pool A reference to a string in ; otherwise , Will be in String Pool Adds a new string to , And returns a reference to the new string .

 Insert picture description here

Illustrate with examples :

String a = new String("aaa");
String b = new String("aaa");
System.out.println(a == b);     // false
//  Use intern Get string reference 
String c = a.intern();
String d = a.intern();
System.out.println(c == d);     // true
//  If it is the following creation string , Will be put directly into the string pool 
String e = "aa";
String f = "aa";
System.out.println(e == f);     // true

Popular speaking ,intern() The method is to get the string reference , If not, put it in the pool , Return reference , If so, it will directly return the reference .

So we can use when we assign a string intern() To save space by referencing .

原网站

版权声明
本文为[Program diary]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/176/202206252101115926.html