The difference between heap and stack of Java learning

Source: Internet
Author: User

First, stack

1. Different ways of disappearing: Method variables are released as the Stack method is released

2. The storage location is different, the default replication processing mechanism is different: will not give the property of the method to the initial value, it can be understood that the properties of the method in the class is a local variable, the local variable cannot be attached to the initial value, the state of the class is represented by the values of the member variables of the class, so the class is is stateless, and the stored space is within the stack method area.

Second, the heap

1. member variables will not disappear with no reference, waiting for the JVM's garbage collector to recycle, that is, the JVM's GG

2. The member property of the instantiated object is appended with the initial value

3. As long as it is new, the memory will open up the space

4. Erase the reference, even if the reference is named NULL, as follows:

 Packagecom.dreamly.day01;/** * @authordreamly **/ Public classArrayTest01 { Public Static voidMain (string[] args) {int[] x=New int[3]; x[0]=2; X=NULL;//erases the connection to the instance object, that is, the address value is not saved.     }}

The difference between heap and stack of Java learning

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.