The string belongs to the object and not to the base data type, why?

Source: Internet
Author: User

The String Class (Java.lang.String) is the most used class in Java and the most special class, and many times, we are both familiar and unfamiliar. First, fundamentally understand the Java.lang.String class and the string pool Firstly, I suggest that we first look at the source code implementation of the String class, which is the fundamental starting point to understand the string class in essence. As you can see: 1, the String class is final and cannot be inherited. Public final class String. 2, the String class is the essence of the character array char[], and its value cannot be changed. PRivate final char value[]; then open the API document of the string class, you can see: 3, the String class object has a special way to create, that is, directly specify such as String x = "abc", "ABC" represents a String object. X is the address of the "ABC" object, also called a reference to the "ABC" object. 4. String objects can be concatenated by "+". A new string is generated after concatenation. It can also be concatenated by concat (), which is explained later. 6. The Java runtime maintains a string pool, Javadoc translation is very vague "string buffer". The string pool is used to hold the various strings produced in the runtime, and the contents of the strings in the pool are not duplicated. The normal object does not exist in this buffer pool, and the object created only exists in the stack area of the method. 5, there are many ways to create strings, summed up there are three categories: one, using the new keyword to create a string, such as string s1 = new string ("abc"), and the second, directly specified. For example, string s2 = "abc"; third, a new string is generated using concatenation. For example, string s3 = "AB" + "C"; Second, the creation of a string object is also very important to create a string object, the key is to understand its principle. Principle 1: When using any way to create a string object s, the Java runtime (the running JVM) will hold this x in the string pool to find if there is a string object of the same content, if not, create a string s in the pool, otherwise, not add in the pool. In principle 2:java, whenever you use the New keyword to create an object, you must create a new object (in the heap or stack area). Principle 3: Creating a String object using direct designations or concatenation with a pure string only checks the string in the maintenance string pool without creating one in the pool. However, the string object will never be created in the stack area again. Principle 4: Using an expression containing a variable to createString object, it will not only examine the maintenance string pool, but also create a string object in the stack area. In addition, the Intern () method of string is a local method, defined as public native String intern (); The value of the Intern () method is to allow the developer to focus on the string pool. When the Intern method is called, if the pool already contains a string equal to this string object (which is determined by the Equals (object) method), the string in the pool is returned. Otherwise, this string object is added to the pool, and a reference to this string object is returned. The immutable class immutable string has a great advantage: The compiler can set the string to be shared. Immutable classes string has an important advantage-they are not shared references. So, Java, for the sake of efficiency, has been specially handled for string types--providing a string type of string that defines a variable of type string in two ways: string name= "Tom"; String name =new string ("Tom") uses the first method when using a string pool, when using the second way, is a normal way to declare objects if you use the first way, then when you declare a content is "Tom" string, It will use the original memory in the pool without reallocating the memory, that is, string saname= "Tom", which will point to the same piece of memory. Additionally, the string type is immutable: The string type is immutable, meaning that When you want to change a string object, such as Name= "Madding" then the virtual machine will not change the original object, but instead generate a new string object, and then let name point to it, if the original "Tom" does not have any object to reference it, The garbage collection mechanism of the virtual machine will receive it. It is said that this can improve efficiency!!!

The string belongs to the object and not to the base data type, why?

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: 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.