The variables of Java and the storage location differences between objects

Source: Internet
Author: User

Java memory allocation involves a total of 5 regions, namely, registers, stacks, heaps, Chang, non-RAM storage.

Register

Registers are the fastest storage, located inside the processor, because the number of registers is very limited, so registers are on demand, we have no control in the program.

Stack

The stack is in general RAM (random access memory), the stack is automatically allocated and freed by the compiler, and the stack holds the base data type (basic data types include: int, short, double, long, float, Boolean, char, byte; Note No string) and a reference to the object. The advantage of the stack is that the access speed is faster than the heap, second only to the registers in the CPU. In addition, a big feature of the stack is that the data in the stack can be shared, such as defining two variables of type int: int a = 3; int b = 3; Here A and B are a reference to the int type, pointing to the literal value of "3". The compiler processes int a = 3 first, when the statement creates a reference to the variable A in the stack, and then looks for an address that has no literal value of 3, and if not, opens a storage 3 address, then points a to the 3 corresponding address. then the int b = 3 is processed, and a reference to the variable B is created first, since there is already a literal of 3 in the stack, so the B also points to the corresponding address of the 3, so a and b all point to an address. When we execute B = 4, the first thing to do is to go to the stack and look for an address that has no literal value of 4, and if not, open it, and then point B to the new open address. If you already have a direct B point to the address, at this point a still points to 3, but B points to 4, and they no longer point to the same address. In fact, local variables are stored in the stack.

Heap

A heap is a common memory pool, also in RAM, used to store all Java objects.

Constant pool

Constant pools are used to hold constants

Non-RAM Storage

Non-RAM storage to hold persisted objects

Let's look at an example:

public class Student {/** * @author runner Moro * @ Blog link http://blog.csdn.net/xingzhemoluo */private int id;private String NAME;PR Ivate int Age;//get,set method omit public Student (int i,string n,int a) {this.id = I;this.name = N;this.age = A;}}
public class Test {public void update (int x) {x = 100;} public static void Main (string[] args) {int score = 99; Test test = new test (); Test.update (score); Student Student = new Student (1, "Walker", 23);}}
In this code, score is a local variable, I, N, a, x are formal parameters, parameters are local variables, ID, name, age is a member variable. When you start running from the Main method,

①int score = 99; Score is a local variable, and the reference (score) and value (99) are stored in the stack.
②test test = new test (); Test is a reference, stored in the stack, and the object created by new test () exists in the heap.

③test.update (score); The parameter X in the update () method is a local variable, both the reference and the value are present in the stack, and when the Update method finishes executing, X automatically disappears from the stack.

④student Student = new Student (1, "Walker", 23); Student as a reference, stored in the stack, new Student (1, "Walker", 23) created objects exist in the heap. where I, n, a are formal parameters, exist in the stack, name,id,age are member variables, their corresponding values are stored in the heap (in New Student (). When the student construction method finishes executing, I, N, and a will automatically disappear from the stack.

After the ⑤main method is executed, the score, test, and Student will automatically disappear from the stack, and new test () and new Student (1, "Walker", 23) are reclaimed by the garbage collection period.


Reference: http://blog.csdn.net/rj042/article/details/6871030

http://blog.csdn.net/cuixianpeng/article/details/6892043

Statement:

1. Original content, reproduced please specify the original link

2. Moro does not blog, all content is only the cat notes, if there are errors or you have a better opinion, please correct me.


Welcome to follow the public number (Xingzhemoluo), common communication programming experience, scan the following QR code can be;




The variables of Java and the storage location differences between objects

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.