Java Basics | heap and Stack

Source: Internet
Author: User

One, Java six places to store data

1) Register: Thisis the fastest storage area, because it is located in a different place than the other storage-the inside of the processor. However, the number of registers is extremely limited, so the registers are allocated by the compiler on demand. You cannot directly control or feel any sign of register presence in the program.

2) Stack:in general Ram, this is a fast and efficient method of allocating storage, second only to registers. A reference to a variable and an object, an array, that holds the base data type.

3) Heap:also located in general purpose RAM, for storing new objects, it takes more time to store the heap than to store it on the stack.

4) static storage (staticstorage): Here "static" means "in a fixed position". In static storage, the data that persists while the program is running, that is, the property of the keyword static identity, but the Java object is not stored in the static storage space.

* Why non-static methods, can not use static properties?

Because static methods and static properties are compiled after a non-static method, when the non-static method is compiled and used, static properties are not generated, resulting in an error!

5) constant Storage (constant storage): Constant values are usually stored directly inside the program code, and it is safe to do so because they are never changed. Sometimes in an embedded system, the constants themselves are separated from the rest, so in this case, you can choose to place them in ROM. Therefore, constants store string constants and basic type constants that are used to hold the final keyword decorations.

6) Non-RAM storage: If the data is completely out of the program, it can be free from any control of the program and can exist when the program is not running.

Second, heap and stack

Same point: All used to store data

Different points:

1) Stacks are used to hold the variables and objects of the base data type, the reference to the array, and the heap is used to hold the new object.

2) The data stored in the stack, size and lifetime must be determined, lack of flexibility. The data stored in the heap does not have to know the size of the data, and does not need to know the life cycle, high flexibility, resulting in slower

3) Life cycle

When a method executes, each method builds its own memory stack, and the variables defined in this method are placed in the stack memory and destroyed as the method ends (no GC is required).

When the new object is executed, the data is generated in the heap, and the garbage collector (GC) of the system reclaims it when it is appropriate only if the object does not have any reference variables to reference it.

4) The operation of the stack solution, that is, how the program executes, or how to process the data, the heap solves the problem of data storage, that is, how to put the data and where.

/*** Car Class*/ Public classCar {//numbering    Private intID; //Car name    PrivateString name; //Speed    Private DoubleSpeed ;  Public intgetId () {returnID; }     Public voidSetId (intID) { This. ID =ID; }     PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }     Public Doublegetspeed () {returnSpeed ; }     Public voidSetspeed (DoubleSpeed ) {         This. Speed =Speed ; }     PublicCar (intID, String name,DoubleSpeed ) {         This. ID =ID;  This. Name =name;  This. Speed =Speed ; }     PublicCar () {} @Override PublicString toString () {return"car{" + "id=" + ID + ", name=" + name + "\" + ", speed=" + Speed + ‘}‘; }}
View Code
        int i = 1;         New Car (1, "BMW", 200);
View Code

Graphical Analysis:

Keep looking at the code below and think about the output value

        int i = 1;         New Car (1, "BMW", $);         = car;        Newcar.setspeed (+);        System.out.println (Car.tostring ());        System.out.println (Newcar.tostring ());
View Code

Graphical Analysis:

Car Newcar = car in the code, just give the referenced address to Newcar, they point to the same object in the heap, so when Newcar changes the object, the object that the car points to is also changed.

Sometimes in code, we don't want two variables to point to the same object, and you can use clone to re-generate the same object in the heap.

Java Basics | heap and Stack

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.