A detailed explanation of the string object creation mechanism in Java (interview must be asked)

Source: Internet
Author: User
Tags constant stringbuffer advantage

One, string uses private final char value[] to implement string storage

So string objects can no longer modify the contents of the strings stored in this object after they are created, so the string essence is a character array char[] and its type is immutable .

The corresponding String,stringbuffer is roughly the same as string functionality, but the implementation method is different, StringBuffer is variable and thread-safe.

the creation method of string in Java (four kinds)

1, directly use "" double quotes to create; (String S1 = "a";)

2. Create with new String () (String s2 = new string ();)

3, using new String ("string") to create; (String s3 = new String ("string");)

4, the use of overloaded string connectors to create; (String s4 = "I" + "second";)


third, before you delve into the string creation mechanism, you need to understand an important concept: Chang (Constant Pool)

In a Java-compiled bytecode file. class file, there is a region called constant Pool, a table of arrays, used to store various constants in the program, including class, String, Integer, and various Java basic data types;

String pool is the region in the constant pool where string constants are stored;


Use the "" "double quote creation mechanism directly

The method created with double quotes is a very special way to create, such as the following code:

String S1 = "a";
STIRNG s2 = "a";
System.out.println (S1 = = s2);
1, compile period: "A" is a compile-time constant, the compile period can confirm its value, in the compiled. Class bytecode file, "the" is already in the string pool;

2, Runtime: The JVM will only look for the maintenance constant pool , holding "a" in string pool to find if there is a string with the same content (confirmed by the Equals () method), and if so, returns a reference to the corresponding memory unit in string pool. Assign to S1 (S1 is the address that holds the "a" memory unit in string pool); If not, create a "one" in string pool, return the reference, assign the value to the S1;S2;

This process is actually called Intern () method implementation;

In this process, the JVM will never create a string object in the heap area (heap) ;

So, the code above, S1 and S2 point to the same memory area in string pool, is the same object, so returns True.


v. Creating mechanism with new string ("string")

The equivalent of creating two string objects, one at a string pool, one in the heap area (heap);

string S1 = new String ("a");
String s2 = "a";
System.out.println ("S1 = = s2");
In Java, a new object is created using new keyword, regardless of whether there are objects with the same value in string pool. always create a new string object to store in the heap area (heap), and then returns a reference to the corresponding memory unit in the stack area (heap), assigning it to S1, and S2 or pointing to the corresponding internal deposit in string pool;

So S1 and S2 are definitely not the same object, but only store string values are the same, so return false.


VI. Java Memory model

The JVM has a heap (heap), a heap is the Run-time data area, and dynamically allocates memory (the heap's advantage)for all class instances and arrays, the data lifecycle does not have to be told to the compiler, and the release of the memory is automatically recycled by the GC (garbage disposal mechanism), but The access efficiency of the heap is lower (the disadvantage of the heap);

Compared to heaps, stack access is fast (the stack's advantage), second only to registers, which are typically used to hold references to basic type variable data such as int, short, long, byte, float, double, Boolean, Char, and objects. The data in the stack can be shared, but the data size and lifecycle in the stack must be fixed and inflexible (the drawbacks of the stack).



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.