Java Virtual Machine Storage area

Source: Internet
Author: User

The method area and the heap area are data sharing areas.

Stack area: data is not shared. Method parameters, local variables, intermediate results of participating operations, return values, and so on are all in the stack area.

Heap area: Data sharing. Store the object.

The method area holds type information, type information includes: field information, method information, constant pool of that type, class variables, a reference to a class ClassLoader, and a reference to the class. This part of the data is shared and is a Java Virtual machine instance or a Java program that is shared.

When a class is loaded by multiple different classloader, it needs to be identified to the ClassLoader. Also identify a class, his fully qualified name.

 Public classTest {intA; intb;  Public Static voidMain (string[] args) {intc = 20; Test test=NewTest ();        Test.change (TEST,C); System.out.println (Test.a+ "First" +c); }         Public voidChange (Test T,intc) {C=50; T.A=10; T=NewTest (); T.A=20; System.out.println (T.a+ "Second"); }}

The result is:

Second
Ten first 20

The main method and the change method will open two stack frames in the Java stack, a method corresponding to a stack frame, the current stack frame represents the call of the current method, each stack frame will have his data structure, his structure is in the previously mentioned stack area to store things.

The main method accepts an args parameter and defines two local variables.

int c = 20; Then a variable with a value of 20, named C, is defined on this stack frame. For the time being, it would be better to look at the timing of his execution.

Test test = new test ();

That's a lot more to do with new things. First look to the right, he opens up a storage space in the heap area, which has address values, and this space holds a reference to the type information in the method area. Then the space in the method area describes the type information for that class, including fields, methods, class variables, and so on.

On the left is a reference to a test type that holds the address value of the heap area.

When the main method calls change, does it pass it over? In fact, the value passed, the object's address value and 20 passed.

The change method accepts these two parameters, and he opens up two blocks of storage space for both parameters in the stack to store the two parameters, where there is no local variable defining the method.

At this time t= pass in the address value, c=20;

Now the changes are coming, changing their values. C=50: Whose value does he change here? is actually changing the value of the variable that belongs to the stack frame of the change method. Instead of changing the value of the variable to the main method stack frame. So at the time of printing C is still 20, do not be confused by the variable name.

Next: t.a=10; This is the first object that is passed in. His A has been changed, equal to 10.

Next step: t = new Test (), this time the heap has two objects, the value of the T is no longer the address value of the previous object, but the new object's address value, the new object also has its own member fields and methods, and so on.

The last t.a=20 change is the A variable of the new object, not the previous one.

So that's the result.

In Java, the transfer of method parameters is a value, the reference data type is the address value, the string is "content", the basic data type is the normal value.

Java Virtual Machine Storage area

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.