The String class in Java

Source: Internet
Author: User

Tag: Contains the string class equality execution equivalent array high performance obj new

    • What are the characteristics of the string class?
    1. The string class is the final class, meaning that the string class cannot be inherited, and its member methods default to the final method.
    2. The string class actually holds strings through a char array.
    3. Once the string object is created, it is fixed, and any changes to the string object do not affect the original object, and any change operations related to it will generate a new object.
    • string constant pool?
    1. We know that the allocation of strings, like other object assignments, takes a lot of time and space, and strings we use very much. In order to improve performance and reduce the overhead of memory, the JVM has some optimizations when instantiating strings: using a string constant pool. Whenever we create a string constant, the JVM first checks the string constant pool, and if the string already exists in the constant pool, it returns the instance reference in the constant pool directly. If the string does not exist in a constant pool, the string is instantiated and placed in a constant pool. Because of the immutability of string strings we can be quite certain that there must not be two identical strings in a constant pool.

String a= "AA";

String b= "AA";

String C=new string ("AA");

A, B, and literally AA are all "AA" objects that point to the JVM string constant pool, and they point to the same object.

The new keyword will definitely produce an object AA, and this object is stored in the heap. So the above sentence should produce two objects: the AA in the string constant pool in the method area and the AA in the Save heap. But there are no two exactly identical string objects in Java. So the AA in the heap should be a reference string in the constant pool of AA. So the relationship of C, heap AA, pool AA should be: c---> Heap aa---> Pool aa.

Although a, B, C, and C are different references, we can understand the above from the internal structure of string. String c = new String ("AA"), although the content of C is created in the heap, but his internal value is also the value of AA to the JVM constant pool, and the parameter it uses to construct AA is still the AA string constant.

What are the advantages and disadvantages of string pooling?
    • The advantage of a string pool is that it avoids the creation of strings of the same content, saves memory, eliminates the time to create the same string, and improves performance; On the other hand, the disadvantage of a string pool is the time it takes for the JVM to traverse objects in a constant pool, but its time cost is relatively low.

equals and = =?
    • for = =, if a variable (Byte,short,char,int,long,float,double,boolean) that is acting on the base data type is directly compared to its stored value equality, or to a variable of reference type (String), The address of the object being pointed to (that is, whether to point to the same object) is compared.
    • The Equals method is a method in the base class object, so there is a method for all classes that inherit from object. In the object class, the Equals method is used to compare whether the reference to two objects is equal, that is, to point to the same object.
    • For the Equals method, note that the Equals method cannot act on a variable of the base data type. If the Equals method is not overridden, the address of the object to which the variable of the reference type is to be compared, and the string class overrides the Equals method to compare whether the strings stored by the pointed string object are equal. Other classes, such as Double,date,integer, have overridden the Equals method to compare whether the object being stored by the pointer is equal.

How do I create a string?
    1. The strings created by using the "" quotation marks alone are constants, and the compilation period is determined to be stored in the string pool;
    2. Objects created with the new String ("") are stored in the heap and are newly created at run time;
    3. A constant is created with a string connector that contains only constants, such as "AA" + "AA", and is determined by the compilation period and is determined to be stored in the string pool;
    4. The object created by using a string connector containing a variable, such as "AA" + S1, is created at run time and stored in the heap;

What is the difference between String, StringBuffer, StringBuilder?
    • Mutable and immutable: string is an immutable string object, StringBuilder and StringBuffer are mutable string objects (their internal character array length is variable).
    • Multithreading security: Objects in a string are immutable and can be understood as constants, apparently thread-safe. The methods and functions in StringBuffer and StringBuilder are completely equivalent, but the methods in StringBuffer are mostly decorated with synchronized keywords, so they are thread-safe, and StringBuilder Without this modification, it can be considered non-thread safe.
    • The execution efficiency of String, StringBuilder, and StringBuffer are as follows:
    • StringBuilder > StringBuffer > String Of course this is relative, not necessarily in all cases. For example, string str = "Hello" + "world" is more efficient than StringBuilder st = new StringBuilder (). Append ("Hello"). Append ("World") is higher. Therefore, these three classes are each with pros and cons and should be used according to different circumstances:
    • It is recommended to use string str= "Hello" in the case of a string addition or less modification;
    • It is recommended to use StringBuilder when the string addition operation is large, and stringbuffer if multi-threading is used.

The String class in Java

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.