Sword refers to Offer-40-Rotate the string left and take a look at the difference between StringBuffer and StringBuild while it is hot

Title description

There is a shift instruction in assembly language called Rotate Left (ROL). Now there is a simple task, which is to simulate the result of this instruction with a string. For a given character sequence S, please output the sequence after circularly shifting it K bits to the left. For example, the character sequence S="abcXYZdef", it is required to output the result of the circular shift left by 3 bits, that is, "XYZdefabc". Is not it simple? OK, get it done!

Idea analysis

violence

Code

public class Solution {
    
    
    public String LeftRotateString(String str,int n) {
    
    
        if(str==null||str.length()==0) return "";
        StringBuffer sb = new StringBuffer();
        char[] array = str.toCharArray();
        for(int i = n;i<array.length;i++){
    
    
            sb.append(array[i]);
        }
        for(int i = 0;i<n;i++){
    
    
            sb.append(array[i]);
        }
        return sb.toString();
    }
}

Take advantage of the heat to see the difference between StringBuffer and StringBuild

First of all, when it comes to StringBuffer and StringBuild, we must first look at String.

Several points to note about String:

  • String rewrites the equal method. Therefore, its equal is not the address pointed to by the reference type being compared, but the actual stored value of the comparison.
  • String source code:
    /** The value is used for character storage. */
    private final char value[];

What does that mean? This means that the String class is stored using a character array modified by the final keyword. Push immediately, String objects are immutable.

  • Note that the String class of JDK1.9 uses a byte array for storage.
  • private final byte value[]

StringBuffer和StringBuild

  • First look at the underlying source code of the two:
 public final class StringBuffer
    extends AbstractStringBuilder
    implements java.io.Serializable, CharSequence
    {
    
    

    /**
     * A cache of the last value returned by toString. Cleared
     * whenever the StringBuffer is modified.
     */
    private transient char[] toStringCache;

    /** use serialVersionUID from JDK 1.0.2 for interoperability */
    static final long serialVersionUID = 3388685877147921107L;

    /**
     * Constructs a string buffer with no characters in it and an
     * initial capacity of 16 characters.
     */
    public StringBuffer() {
    
    
        super(16);
    }
public final class StringBuilder
    extends AbstractStringBuilder
    implements java.io.Serializable, CharSequence{
    
    
      /** use serialVersionUID for interoperability */
    static final long serialVersionUID = 4383685877147921099L;

    /**
     * Constructs a string builder with no characters in it and an
     * initial capacity of 16 characters.
     */
    public StringBuilder() {
    
    
        super(16);
    }
  • Both are inherited from the source code AbstractStringBuilder. In the parent class, string arrays are also used to store strings. But there is no final modification like String, so it is variable.

  • They all call the constructor of the parent class.

  • String is a constant and is thread-safe.

  • StringBuffer adds a synchronization lock to the method or adds a synchronization lock to the called method, so it is thread-safe.
    Insert picture description here

  • StringBuilder is not locked, so thread is not safe.
    Insert picture description here

  • Insert picture description here

  • Insert picture description here
    Reference: The difference between JavaGuide StringBuffer and StringBuild

Guess you like

Origin blog.csdn.net/H1517043456/article/details/107451872