Java Basics (a) String, StringBuffer, StringBuilder

Disclaimer: This article is a blogger original article, please indicate the source. Welcome to public concern number: Java Notes Share (xiaosen_javashare) https://blog.csdn.net/qq_36447151/article/details/80392682
 

 

Do not look too long version

 

    String concatenation speed: StringBuilder> StringBuffer> String

    StringBuilder StringBuffer non-thread-safe thread-safe

text

A, String class

    . 1, String class is immutable class, String once created, the character sequence included in this object is immutable, until the object is destroyed.

    

String String class by storing an array of char.

/** 用于字符存储. */
    private final char value[];
/**
     * Initializes a newly created {@code String} object so that it represents
     * an empty character sequence.  Note that use of this constructor is
     * unnecessary since Strings are immutable.
     */
    public String() {
        this.value = "".value;
    }

In other methods of String, String returned is new new (); String it to operate, and returns a new string.

2, the string constant pool

It maintains a String Pool (String pool) Java Runtime, also called string buffer. Various string generated when the storage operation, and the cell strings are not repeated.

3, String object is created Principle

 

    1) When to create a string object s when using any means, Java Runtime (running JVM) will be holding this String s looking at whether the same pool of content string object exists, and if not, then in the pool create a string s, otherwise it is not in the pool added.

    2) Java, as long as the object is to be created using the new keyword, it will (create a new object in the heap).

    3) Use specified directly or using pure string concatenation to create a String object, the only maintenance checks string String pool, the pool is not created in a pool, if not created, but never in the stack area to create a String object again.

    4) to create a String object using an expression containing variables, not only checks the String pool maintenance, but also create a String object in the stack area. The last object in memory pointing to the heap.

    @Test
    public void stringTest(){
        String s1 = "hello world";
        String s2 = "hello world";
        String s3 = new String("hello world");
        System.out.print(s1==s2);
        System.out.print(s1==s3);
        System.out.print(s2==s3);
    }

The result is true, false, false.

Two, StringBuffer and StringBuilder

    Inherit AbstractStringBuilder class, implementation and use are the same. The only difference is, StringBuffer methods add synchronized keyword, thread-safe, but StringBuilder is a non-thread-safe, but StringBuilder string concatenation faster than StringBuffer

 

    StringBuilder StringBuffer compared to the speed advantage, it is recommended to use the StringBuilder class in most cases. However, in the case of application requires thread-safe, you must use the StringBuffer class.

Welcome to public concern number:

 

 

Guess you like

Origin blog.csdn.net/qq_36447151/article/details/80392682