A very basic introduction about java stack and heap

Source: Internet
Author: User
Forwarded fromHttp://www.javatutorialhub.com/wiki/Stack_and_Heap
Stack and heap

The JVM divided the memory into following sections.

    1. Heap
    2. Stack
    3. Code
    4. Static

This division of memory is required for its proactive management.

The Code section contains your bytecode.

The stack section of memory contains methods, local variables and reference variables.

The heap section contains objects (may also contain reference variables ).

The static section contains static data/methods.

Of all of the above 4 sections, you need to understand the allocation of memory in stack & heap the most, since it will affect your programming efforts

Difference between instance variables and local variables

Instance variables are declared inside a class but not inside a method

 
Class student {
 
Int num; // num is instance variable
 
Public void showdata {}

Local variables are declared inside a method including method arguments.

 
Public void sum (int ){
 
Int x = int A + 3;
 
// A, X are local variables
 
}

The following video demonstrates how memory is allocated in stack & heap.

Click to watch video

Please be patient. Video will load in some time. If you still face issue viewing video clickhere

Points to remember:
    • When a method is called, a frame is created on the top of stack.
    • Once a method has completed execution, flow of control returns to the calling method and its corresponding stack frame is flushed.
    • Local variables are created in the stack
    • Instance variables are created in the heap & are part of the object they belong.
    • Reference variables are created in the stack.

Point to ponder:

What if object has a reference as its instance variable?

Public static void main (string ARGs []) {

 
A parent = new ();
// More code

}

 
Class {
 
B child = new B ();
 
Int E;
 
// More code

}

 
Class B {
 
Int C;
 
Int D;
 
// More code

}

In this case, the reference variable "child" will be created in heap, which in turn will be pointing to its object, something like the dimo-shown below.

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.