Home > Back-end >  Deep understanding of Java: String
Deep understanding of Java: String

Time:12-07

Before explaining the String, we first look at the Java memory structure,

A, the Java memory model

Officially: Java virtual machine has a pile, pile is runtime data areas, all class instance and an array of memory from distribution,
Main two types of Memory: the JVM Heap and the Heap, the Heap Memory (Heap Memory) is created in the Java virtual machine startup, the Heap Memory (Non - Heap Memory) is outside of the JVM Heap Memory,
In simple terms, the pile contains methods section, the JVM internal process or optimize the required memory (such as JITCompiler, Just - in - time Compiler, instant compiled code cache), each class structure (such as run time constant pool, data fields and methods) as well as the method and the constructor code,
Java heap is a runtime data area, the class (object to allocate space, these objects through new, newarray, anewarray and multianewarray instruction set up, they don't need to program code to explicitly release, heap is responsible by the garbage collection, the advantage of the pile can be dynamically allocated memory size, lifetime also don't have to tell the compiler in advance, because it is a dynamically allocated memory at run time, the Java garbage collector automatically these are no longer used to collect data, but the disadvantage is that due to dynamically allocated memory at run time, access speed is slow, is the advantage of stack, access speed faster than the heap, second only to registers, stack data can be Shared, but the disadvantage is that there is data in a stack size and survival period must be determined, the lack of flexibility, main storage stack of some basic types of variable data (int, short, long, byte, float, double, Boolean, char) and object handle (reference),
Virtual machines must be loaded for each type of maintaining a constant pool, constant pool is the type used constants of an ordered set, including direct constants (string, integer and floating point constants) and for other types of field and Method of symbol references, for string constants, its value is in the constant pool, and the constant pool in the JVM in memory in the form of table, for type string, there is a fixed length CONSTANT_String_info table used to store text string value, note: this table only store the text string value, are not stored symbol references, speaking of which, the string value of constant pool of storage location should have a more clear understanding, at the time of program execution, the constant pool will be stored in Method Area, rather than in the heap, in the constant pool holds many string object; And can be Shared to use, so it improves the efficiency of
Specific knowledge about the JVM and memory please refer to:
Basic knowledge of the JVM
Java memory model and GC principle

Second, the case analysis
 public static void main (String [] args) {
/* *

* scenario 1: string pool* the JAVA virtual machine (JVM) exists in the pool, a String of preserved many String object;
* and can be Shared to use, so it improves the efficiency,
* because the String class is final, its value cannot be changed once created,
* String pool maintained by the String class, we can call the intern pool () method to access the String
*/
String s1="ABC";
//write in pool creates a string object
String s2="ABC";
//write string pool existing object "ABC" (share), so creating zero objects, accumulative total to create an object
System. The out. Println (" s1==s2: "+ (s1==s2));
//write true point to the same object,
System. The out. Println (" s1. Equals (s2) : "+ (s1) equals (s2)));
//write true value equal to
//write -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- over
/* *
* scene 2: about the new String (" ")
*
*/
String s3=new String (" ABC ");
//create two objects, a stored in a string in the pool, a being and heap area;
//write another object reference s3 stored in a stack of
String s4=new String (" ABC ");
//write a string in the pool "ABC" existing objects, so only in the heap object creates a
System. The out. Println (" s3==s4: "+ (s3==s4));
//write false s3 and s4 address different stack area, point to pile in different address;
System. The out. Println (" s3. Equals (s4) : "+ (s3) equals (s4)));
//write true s3 and s4 of the same value
System. The out. Println (" s1==s3: "+ (s1==s3));
//write false storage areas more different, a stack area, a pile of zone
System. The out. Println (" s1. Equals (s3) : "+ (s1) equals (s3)));
//write true value same
//write -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- over
/* *
* scene 3:
* due to the constant values were determined at compile time (optimization),
* here, "ab" and "CD" is constant, so the value of the variable str3 can determine at compile time,
* this line of code compiled effect is equal to: String str3="abcd";
*/
Strings str1="ab" + "CD";//object 1
String str11="abcd";
System. The out. Println (" str1=str11: "+ (str1==str11));
//write -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- over
/* *
* scenario 4:
* local variable str2 str3 store is two detained a string object (intern string objects) address,
*
* the third principle of lines of code (str2 + str3) :
* the run-time JVM will first create a StringBuilder class in the heap,
* at the same time complete the initialization with str2 refer to the detention of the string object,
* then calls the append method to detain the str3 points to a string of merger,
* StringBuilder then call the toString () method creates a String object in the heap,
* will finally just generated String object heap address in local variable str3,
*
* the str5 storage is string pool "abcd" corresponds to the detention of string objects address,
* str4 str5 address and not the same, of course,
*
* in memory, in fact, there are five string objects:
* three detained a String object, a String object and a StringBuilder objects,
*/
String str2="ab";//object 1
String str3="CD";//object 1
String str4=str2 + str3;
String str5="abcd";
System. The out. Println (" str4=str5: "+ (str4==str5));//false
//write -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- over
/* *
* scene 5:
* the JAVA compiler basic types of string +/constant is evaluated as a constant expression directly to optimize,
* runtime two string together, can produce new objects, stored in a stack (heap)
*/
String str6="b";
String str7="a" + str6;
String str67="ab";
System. The out. Println (" str7=str67: "+ (str7==str67));
//write str6 as variables, at runtime will be parsed,
Final String str8="b";
String str9="a" + str8;
String str89="ab";
System. The out. Println (" str9=str89: "+ (str9==str89));
//write str8 as constant variables, compile time will be optimized
//write -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- over
}

1. The String class is immutable after initialization (immutable)

This Monday again to say a lot, you just know String instance once generated will not be changed, for example: String STR="kv" + "ill" + "" +" ans "; Is has four string constants, the first "kv" and "ill" created "kvill" exists in memory, and then "kvill" and "" generate" kvill "exists in memory, then and generates" kvill ans "; And the address is assigned to the STR String, is because the String "immutable" produced a lot of temporary variables, which is why suggested StringBuffer original sings, because StringBuffer is to change, the following are some common problems on String: a String of final usage and understand the final StringBuffer a=new StringBuffer (" 111 "); Final StringBuffer b=new StringBuffer (" 222 "); a=b;//this sentences to compile the final StringBuffer a=new StringBuffer (" 111 "); A.a ppend (" 222 ");//compiled by visible and final only for reference of "value" (that is, memory address) effectively, it forces references to point to the initial point to the object, change it to can lead to a compile-time error, as far as it points to the change of the object, it is not responsible for the final,

2. In the code string constants in the process of compiling collected and put in a constant section of the class files, such as "123", "123" + "456" and so on, contains variable expression is not included, such as "123" + a,

3. The JVM when the class is loaded, according to the string to generate constant pool in constant area, each character sequences such as "123" will generate an instance in the constant pool, in this instance is not heap, also won't by GC, the instance of the value attribute from the source constructor should be use new build array in 123, so according to my understanding of the value stored at this time is an array of characters address in a heap, welcome to correct me if wrong,

4. Use the String does not necessarily create objects

nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related