Java Review Note 11--stack&heap

Source: Internet
Author: User

Stack stacks & heap Heap

1. Stacks and heaps (heap) are places that Java uses to store data in RAM. Unlike C + +, Java automatically manages stacks and heaps, and programmers cannot directly set up stacks or heaps.

2. The advantage of the stack is that the access speed is faster than the heap, second only to the registers directly in the CPU. However, the disadvantage is that the size and lifetime of the data in the stack must be deterministic and inflexible. In addition, the stack data can be shared. The advantage of the heap is that the memory size can be allocated dynamically, and the lifetime does not have to tell the compiler beforehand that the Java garbage collector automatically collects the data that is no longer in use. However, the disadvantage is that the access speed is slower due to the dynamic allocation of memory at run time.

    1. There are two kinds of data types in Java.

One is the basic type (primitive types), a total of 8 kinds, namely int, short, long, byte, float, double, Boolean, char (note, and no basic type of string). The definition of this type is through such as int a = 3; Long B = 255L; the form to be defined, called an automatic variable. It is worth noting that the automatic variable is a literal value, not an instance of a class, that is not a reference to a class, there is no class here. such as int a = 3; Here A is a reference to the int type, pointing to the literal value of 3. The data of these literals, due to the size of the known, the lifetime of the known (these values are fixed in a program block, the program block exits, the field value disappears), for the sake of speed, it exists in the stack . The other is the wrapper class data, such as Integer, String, double, and so on, the corresponding basic data types are wrapped up class. These classes of data all exist in the heap , and Java uses the new () statement to tell the compiler that it is dynamically created as needed at run time, so it is more flexible, but the disadvantage is that it takes more time.

Even if the program is running to a block of code that uses new to produce arrays and objects, the heap memory occupied by the array and the object itself will not be freed, and the array and object become garbage when no reference variable points to it, and can no longer be used, but still occupy memory. is released by the garbage collector at a later indeterminate time. This is also the main reason for the memory of Java comparison, in fact, the variables in the stack point to the heap memory variables, this is the pointer in Java!

1. Register: The fastest storage area, which is allocated by the compiler according to the requirements, we can not control in the program.

    1. Stack: A reference to a basic type of variable data and an object, but the object itself is not stored in the stack, but is stored in the heap (the new object) or in a constant pool (the string constant object is stored in a constant pool. )

    2. Heap: Stores all new objects.

    3. Static domain: Holds static members (statically defined)

    4. Chang: Holds string constants and basic type constants (public static final).

    5. Non-RAM storage: Persistent storage space such as hard drives

Java Review Note 11--stack&heap

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.