Differences between stacks (stack) and heap (heap) in Java

Source: Internet
Author: User
Tags garbage collection advantage

  In view of the online stack of Java, there are many opinions, some of the C + + stack also mixed in, and some did not distinguish stack,heap Chinese translation, make me stack as a heap, heap as a stack, chaos. Yesterday to participate in a foreign company's written exam, choose the English answer, the wrong choice, know the answer is heap, but chose stack. Today, we decided to distinguish between the two. &NBSP
  
         first stack,heap the Chinese translation: stack-stack, heap-heap. &NBSP
         in Chinese, stack can be translated as "stack", so I looked directly at the words in the computer terminology that started with stacks and stack: 
        Heap Storage: heapstorage    heap Storage allocation: Heapstorage allocation  Heap Storage Management: Heap storage Management  
          Stack address: Stack addressing    stack transform: Stack transformation  stack memory: Stack memory  stack unit: Stack cell< /strong>
  
           Then, Summarize the differences in Java where heap and stack store data separately. &NBSP
  

Heap (Heap) Stack (Stack)
Features in the JVM Memory Data area Memory Command Area
Storing data Object Instances (1) Basic data types, instruction codes, constants, reference addresses for objects (2)

1. Saving object instances is actually saving the object instance's property value, the property's type and the object's own type tag, and so on, without saving the object's method (the method is the instruction, saved in the stack).

When an object instance is allocated in heap, a 4-byte heap memory address needs to be stored in the stack to locate the instance of the object in heap to facilitate locating the object instance.

2. Basic data types include byte, int, char, long, float, double, Boolean, and short.
A function method belongs to an instruction.

=======================
Refer to the heap and stack in the "difference between Java stacks and stacks" widely circulated on the Internet;
The Java Heap is a run-time data area in which the object allocates space. These objects are established through directives such as new, NewArray, Anewarray, and Multianewarray, and they do not require program code to be explicitly released. The heap is responsible for garbage collection, the advantage of the heap is the dynamic allocation of memory size, the lifetime does not have to tell the compiler in advance, because it is dynamically allocating memory at runtime, the Java garbage collector will automatically take away these no longer used data. The disadvantage is that the access rate is slow due to the dynamic allocation of memory at run time. "
"The advantage of the stack is that the access speed is faster than the heap, after the register, the stack data can be shared." The disadvantage is that the data size and lifetime in the stack must be fixed and inflexible. There are some basic types of variables in the stack (, int, short, long, byte, float, double, Boolean, char), and object handles. "
    
         As you can see, the garbage collection GC is heap for the heap, and the stack is Filo-first in, and last out. Advanced and out, can be automatically released. This will make it clear that the new creation is put into the heap heap.
Related Article

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.