Understanding and explanation of Java stack __java

Source: Internet
Author: User
before we talk about stacks and stacks, let's first take a look at the Java allocation structure to memory. As a Java programmer, you should all know that Java programs are run on the Java Virtual machine is the JVM, all the variables, instances, methods in the program are allocated by the JVM in memory space. so let's take a look at what areas of memory the Java program will have at run time: 1. Register: JVM Internal virtual registers and CPU-related, the program can not control. 2. Stack: an instance of a variable and reference data type used to hold the base data type (instance: a reference variable of a class model new to the entity). 3. Heap: Used to store data generated by program dynamics, eg: entities that are created from the class model need to be aware that the objects we create through the new keyword have only the member variables of that object in the heap memory, not the member methods. Because each class's objects have their own member variables stored in their own heap memory, they share the member methods of that class, so not every time new creates a member method. 4. Constant pool: The JVM maintains a constant pool for each type that has already been loaded, and a constant pool is an ordered set of constants used by that type. A constant pool exists in the heap memory area. 5. Code snippet: Used to hold the code fragment read in the Flushing storage device. 6. Data segment: Used to store static members, including static variables, static constants, static methods, static classes. 7. Method area: Used to store all the functions.


The diagram above simply describes the memory distribution of Java programs when they run in the JVM. Next we'll look at the stack and stack features. Heap: 1.new objects come out with the specified address value. 2. Each variable has a default value. An array of eg:int types, an array of char types, a collection of class types, and so on. 3. When used, the GC does not immediately clean the memory, the GC will scan at idle time whether the memory is still a reference, if it does not exist immediately to recycle, if there is waiting for the next scan. Stacks: The stack memory usually holds the instance of the class and the underlying data type (int byte char long double), and the GC immediately reclaims the memory when the variable is outside the function. for stack memory and heap memory shortcomings are also more obvious, stack memory in the allocation of space is fixed in the immutable, and heap memory in the allocation of space is the dynamic allocation of space. Heap memory because the GC is not immediately reclaimed for discarded memory and the GC uses a tree for reference retrieval, this leads to discarded memory and does not immediately reclaim the unused memory space. I think everyone should have seen such a face test, ask "= =" and "equals" the difference, then we through this problem to illustrate the relationship between heap and stack.


String str1 = "abc"; Define a String variable str1
String str2 = "abc"; Define a String variable str2
String str3 = new String ("abc"); Define a string variable in new way STR3
String STR4 = new String ("abc");//STR4

/**
* Then the problem comes *
* Str1 = = str2? True:false;
* str2 = = STR3? True:false;
* Str3 = = STR4? True:false;
*/

System.out.println ("str1 = = str2?:" + (str1 = = str2)); True
System.out.println ("str2 = = Str3?:" + (str2 = = STR3)); False
System.out.println ("STR3 = = STR4?:" + (STR3 = = STR4)); False
System.out.println ("Str3.equals (STR4)": "+ str3.equals (STR4)); True
For STR1==STR2 values of true and str2 = = STR3 value is False, I think it is easy for everyone, but str3 = = STR4 and str3.equals (STR4) values may not be the same as your results. Let me explain this code fragment: 1.STR1 = = str2 Comparison of this expression, "= =" is a logical operator, comparing two values for the underlying data type is equal, and for reference data types, the value of two reference variables pointing to the consistency of address values in heap memory. A string is a reference data type, but a string is a special reference data type, and when we define a variable of type string, the Java virtual machine will first go to the string constant pool to retrieve whether the value being assigned is present in the string constant pool, in the process of assigning a value. If present, assigns the address in the string constant pool that contains the value to the reference variable, and if not, allocates a memory in the string constant pool to store the assigned value and then assigns the value to the reference variable. So when defining STR1 variables, the JVM allocates memory to the "ABC" Value space in the string constant pool and then gives the value's address to str1 this reference variable, while STR2 has "ABC" in the JVM retrieval string constant pool at the time of the definition. This value also gives the address of the value str2, so the value of str1 = = Str2 is true. 2.STR2 = = Str3 This expression of the comparison process, above we said the "= =" logical operator, compared to the string type of variable comparison is the address, then some people will ask, not to say "ABC" This value has been in the string constant pool exists, But why would the result be false? So we're going to show you how a string type variable is defined in a way that is the most commonly used definition for string name = "", which is defined by allocating the memory space in the string constant pool and returning the address of the value, while string name = new String (); This is a completely different way of defining objects that are defined with the New keyword store space in heap memory rather than in a constant pool, and the process is that the Java Virtual Opportunity first retrieves the existence of the worthwhile memory space in the string constant pool and allocates space to the value if it does not exist. If present, the value (not the memory address) is copied into the heap memory, and then the address of the value in heap memory is given to the reference variable STR3. This will cause the value of str2 = = Str3 to be false. 3.STR3 = = STR4 value is False, and I think you can see from the explanation of 2 that str3 the object referenced by STR4 and the address of the object referenced in the heap memory are inconsistent, all false. the value of 4.str3.equals (STR4) is true, and some people may ask, equals () This method is not also the address of the comparison. My answer is very simple, you can look at the object source code everything will understand, Public Boolean equals (Object obj) {
return (this = = obj);
}
very simple code snippet, note that in the red box, this method returns an expression that uses "= =", and it has been said that the "= =" logical operator compares the address in memory in the reference variable, so the answer may be asked again, but the answer is true. So what I'm saying to you is that we're comparing a variable of type string rather than a variable of type object. Object is a superclass of all classes in Java, including custom classes, and all classes inherit the element string class of the object class, but at different times the string class overrides the Equals () method
public boolean equals (Object anobject) {
if (this = = AnObject) {
return true;
}
if (anobject instanceof String) {
String anotherstring = (string) anobject;
int n = count;
if (n = = Anotherstring.count) {
Char v1[] = value;
Char v2[] = Anotherstring.value;
int i = offset;
int j = Anotherstring.offset;
while (n--! = 0) {
if (v1[i++]!= v2[j++])
return false;
}
return true;
}
}
return false;
}
with the code snippet above, it is easy to see what the value of Str3.equals (STR4) is true, and the final comparison is the value of two reference variables. This is my understanding of heap and stack, it may be very simple, so I hope you can talk about helping each other a lot.

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.