String Memory Allocation

Source: Internet
Author: User

Java divides memory into two types: stack memory and heap memory. Some of the basic types of variables defined in the function and the referenced variables of the object are in the function

Stack memory allocation. When a variable is defined in a block of code, Java allocates memory space for the variable in the stack. When the variable scope is exceeded, Java will automatically

Release the memory space allocated for the variable. The memory space can be immediately used as another variable.

Heap memory is used to store the objects and arrays created by new, and the memory allocated in the heap is managed by the Java Virtual Machine's automatic garbage collector. A number is generated in the heap.

After the group or object, you can also define a special variable in the stack so that the value of this variable in the stack is equal to the first address of the array or object in the heap memory.

Variables are the reference variables of arrays or objects. In the future, you can use the reference variables in the stack in the program to access the arrays or objects in the stack.

An array or an object name. The referenced variable is a common variable. It is assigned to the stack during definition. The referenced variable is released after the program runs out of its scope. Array

And the object itself is allocated in the heap. Even if the program runs beyond the code block where the new statement is used to generate an array or object, the array and object itself occupy no memory

Released. the array and object become junk only when no referenced variable points to it. They cannot be used, but still occupy the memory space. In the next uncertain

The time is removed (released) by the garbage collector ). This is also the reason why Java accounts for memory usage.

In fact, the variables in the stack point to the variables in the heap memory. This is the pointer in Java!


Fully parse the string data type in Java
Listen l 6th, 2008 | Categories: Java | tags: Java

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 a null string or not;

3. String STR = "kvill"; string STR = new string ("kvill"); Difference: here, we will not talk about heap or stack, but simply introduce constants first.

Pool is a simple concept.
The constant pool refers to the data that is identified during the compilation period and saved in the compiled. Class file. It includes classes, methods, interfaces, etc.

A constant in, including a String constant.

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; both "KV" and "ill" are words.

Character String constant. When a string is connected by multiple string constants, it must also be a String constant. So S2 is also parsed as a character during compilation.

String constant, so S2 is also a reference of "kvill" in the constant pool.
Therefore, we can conclude that S0 = S1 = S2; the string created with new string () is not a constant and cannot be determined during the compilation period, so the string created with new string () is not put

In the constant pool, 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 to the new object "kvill" created during runtime because it cannot be determined during the compilation period.

Part of the new string ("ill") cannot be determined during the compilation period, so it is also an application that creates the "kvill" object. If you understand this, you will know why.

The result is displayed.

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 used to expand the constant pool.

One method. When a string instance STR calls the intern () method, Java queries whether there are string constants of the same UNICODE in the constant pool. If yes, its

If not, add a string whose Unicode is equal to STR in the constant pool and return its reference. 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, "using the string. Intern () method can save a string class to a global string table.

If the Unicode string with the same value already exists in this table, this method returns the address of the existing string in the table. If there is no string with the same value in the table

Then, register your address in the table. "If I understand the global string table he said as a constant pool, his last sentence will be." If there is no

If it is a string of the same value, the address will be registered into the table. "It is wrong:

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 (), we will be in the constant pool.

A new "kvill" constant is added. The original "kvill" that is not in the constant Pool still exists, so it is not "registering your own address in 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 =:
This is simply to compare the unicode sequence of the two strings. If the two strings are equal, true is returned, and = is to compare whether the addresses of the two strings are the same.

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.

And then generate the "kvill ans". Then, assign the string address to STR, because the string's "immutable" produces many temporary variables.

The reason why stringbuffer is recommended, because stringbuffer can be changed.

1.
Mxj said:
2017l 11th, 2008 at pm

Such a separate statement string STR = "KV" + "ill" + "" + "Ans"; it should not generate temporary variables. A long string after the equal sign should be in

During compilation, it is directly converted into a constant string and placed in the constant pool ..
A test was conducted:
Source code
Public class test {
Public static void main (string [] S ){
String B = "xxx ";
String A = "BBB" + "CCC" + "DDD ";
System. Out. println (A + B );
}
}
After javac is compiled, use javap to view its machine code.
Public static void main (Java. Lang. String []);
Code:
0: LDC #2; // string xxx
2: astore_1
3: LDC #3; // string bbbcccddd
5: astore_2
6: getstatic #4; // field Java/lang/system. Out: ljava/IO/printstream;
9: New #5; // class Java/lang/stringbuilder
12: DUP
13: invokespecial #6; // method Java/lang/stringbuilder. "" :() V
16: aload_2
17: invokevirtual #7; // method Java/lang/stringbuilder. append :( ljava/lang/
String;) ljava/lang/stringbuilder;
20: aload_1
21: invokevirtual #7; // method Java/lang/stringbuilder. append :( ljava/lang/
String;) ljava/lang/stringbuilder;
24: invokevirtual #8; // method Java/lang/stringbuilder. tostring :() ljava/La
Ng/string;
27: invokevirtual #9; // method Java/IO/printstream. println :( ljava/lang/Str
Ing;) V
30: Return

}
We can see that there is only one constant string that has been compiled into "bbbcccddd.
We can see that the compiler automatically calls stringbuilder to process String concatenation. the compiler is still very AI. (Stringbuilder is a new class of 1.5, and 1.4 should be

Stringbuffer, the difference between the two is described later .)

Should stringbuffer be used for coding? It seems that the compiler automatically concatenates strings and uses stringbuffer for processing ..
In C, do you want to change C/2 to C> 1 to improve efficiency? The Compiler sometimes performs much better than we think ..

By jdk1.5, there is a stringbuilder class, which is faster than stringbuffer. After all, stringbuffer is thread-safe.
But I don't know how to use a test case to test the performance differences between the two classes. The efficiency of the two tests is almost the same. It is estimated that the pressure is not enough ..

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.