I. Java memory allocation form
Java divides memory into two types: stack memory and stack memory.
Both stacks and stacks are places where Java is used to store data in Ram. Unlike C ++, Java automatically manages stacks and stacks, and programmers cannot directly set stacks or stacks.
The Java heap is a runtime data zone and class (the object allocates space from it. These objects are created using commands such as new, newarray, anewarray, and multianewarray. They do not need program code to be explicitly released. The heap is responsible for garbage collection. The advantage of the heap is that the memory size can be dynamically allocated, and the lifetime does not have to be told in advance because the heap dynamically allocates memory at runtime, the Java Garbage Collector automatically collects the unused data. However, the slow access speed is due to the need to dynamically allocate memory during runtime.
The advantage of stack is that the access speed is faster than that of stack, second only to register, and stack data can be shared. However, the disadvantage is that the data size and lifetime in the stack must be fixed, and there is a lack of flexibility. The stack mainly stores some basic types of variables (, Int, short, long, byte, float, double, Boolean, char) and object handles.
A very important feature of stacks is that data in stacks can be shared. Suppose we define both:
Int A = 3;
Int B = 3;
The compiler first processes int A = 3. First, it creates a reference with the variable A in the stack, and then finds whether the value 3 in the stack exists. If no value is found, store 3 and point A to 3. Then process int B = 3. After the referenced variable of B is created, B is directed to 3 because there is already 3 in the stack. In this way, both A and B point to 3 at the same time. At this time, if A is set to 4 again, the compiler will re-search whether there are 4 values in the stack. If not, it will store 4 and make a point to 4; if yes, direct a to this address. Therefore, changing the value of A does not affect the value of B. Note that the sharing of data is different from the sharing of two objects pointing to one object at the same time. (is the implementation mechanism consistent with the copy-on-write in C ++ STL ?) In this case, the modification of a does not affect B. It is completed by the compiler and is conducive to space saving. A variable referenced by an object modifies the internal state of the object, which affects the variable referenced by another object.
Implementation Details of string
1. First, string does not belong to eight basic data types. String is an object.
Because the default value of an object is null, the default value of string is also null, but it is a special object and has some features that other objects do not have.
2. Both new string () and new string ("") declare a new null string, which is an empty string or not null;
3. String STR = "kvill ";
String STR = new string ("kvill"); difference:
Here, a simple concept of constant pool is introduced.
The constant pool refers to the data that is identified during the compilation period and saved in the compiled. Class file. It includes constants in classes, methods, interfaces, and other fields, as well as string constants.
Example 1:
String S0 = "kvill ";
String S1 = "kvill ";
String S2 = "KV" + "ill ";
System. Out. println (S0 = S1 );
System. Out. println (S0 = S2 );
Result:
True
True
First, we need to know that Java will ensure that a String constant has only one copy.
Because "kvill" in S0 and S1 in the example are string constants, they are determined during the compilation period, so S0 = S1 is true; "KV" and "ill" are both string constants. When a string is connected by multiple string constants, it must be a String constant, so S2 is also parsed as a String constant during compilation, so S2 is also a reference of "kvill" in the constant pool.
So we get S0 = S1 = S2;
The string created with new string () is not a constant and cannot be determined during the compilation period. Therefore, the strings created with new string () are not placed in the constant pool and they have their own address space.
Example 2:
String S0 = "kvill ";
String S1 = new string ("kvill ");
String S2 = "KV" + new string ("ill ");
System. Out. println (S0 = S1 );
System. Out. println (S0 = S2 );
System. Out. println (S1 = S2 );
Result:
False
False
False
In Example 2, S0 is still a "kvill" application in the constant pool. S1 is a reference of the new object "kvill" created at runtime because it cannot be determined during the compilation period, s2 is also an application that creates the "kvill" object because it has a new string ("ill") in the second half and cannot be determined during the compilation period; if you understand this, you will know why this result is obtained.
4. String. Intern ():
Another note: the constant pool exists in the. Class file and is loaded by JVM at runtime and can be expanded. The intern () method of string is a method to expand the constant pool. When a string instance STR calls the intern () method, Java checks whether the constant pool has the same UNICODE String constant, if yes, its reference is returned. If no, a string with Unicode equal to STR is added to the constant pool and Its Reference is returned. See example 3.
Example 3:
String S0 = "kvill ";
String S1 = new string ("kvill ");
String S2 = new string ("kvill ");
System. Out. println (S0 = S1 );
System. Out. println ("***********");
S1.intern ();
S2 = s2.intern (); // assign the reference of "kvill" in the constant pool to S2
System. Out. println (S0 = S1 );
System. Out. println (S0 = s1.intern ());
System. Out. println (S0 = S2 );
Result:
False
**********
False // although s1.intern () is executed, its return value is not assigned to S1
True // indicates that s1.intern () returns a reference to "kvill" in the constant pool.
True
Finally, let me get rid of another misunderstanding:
Someone said, "Use string. the intern () method can save a string class to a global string table. If a unicode string with the same value is already in this table, this method returns the address of an existing string in the table, if there is no string with the same value in the table, register the address in the table. "If I understand the global string table he said as a constant pool, his last sentence is, "If there is no string with the same value in the table, register your address in the table" is incorrect:
Example 4:
String S1 = new string ("kvill ");
String S2 = s1.intern ();
System. Out. println (S1 = s1.intern ());
System. Out. println (S1 + "" + S2 );
System. Out. println (s2 = s1.intern ());
Result:
False
Kvill
True
In this class, we do not have a "kvill" constant, so there is no "kvill" in the constant pool at the beginning. When we call s1.intern () then, a new "kvill" constant is added to the constant pool. The original "kvill" that is not in the constant Pool still exists, so it is not "registering your own address to the constant pool.
If S1 = s1.intern () is false, the original "kvill" still exists;
S2 is now the address of "kvill" in the constant pool, so S2 = s1.intern () is true.
5. About equals () and =:
In simple terms, this is to compare whether the unicode sequence of the two strings is equivalent. If the two strings are equal, true is returned, and = is to compare whether the addresses of the two strings are the same, that is, whether it is a reference to the same string.
6. the string is immutable.
This is a lot more important, as long as you know that the string instance will not change once it is generated, for example: String STR = "KV" + "ill" + "" + "Ans ";
There are four string constants. First, "KV" and "ill" generate "kvill" to exist in the memory, and then "kvill" and "generate" kvill "to exist in the memory, finally, "kvill ans" is generated, and the address of this string is assigned to STR, because the string's "immutable" produces many temporary variables, this is why stringbuffer is recommended, because stringbuffer can be changed.