Java knowledge Summary (6)-memory Analysis

Source: Internet
Author: User

1. in Java, variable types other than basic types are called application types. Objects in Java are operated by referencing them.

2. Distinction between classes and objects in the memory:

A. Classes are static and stored inCodeArea;

B. The object is new and located in heap memory (dynamic). Each member variable of the class has different values (except static variables) in different objects ), the method only occupies the memory when executed.

3. regions involved in Java memory allocation:

◆ Register: we areProgramCannot be controlled

◆ Stack: stores basic types of data and object references, but the object itself is not stored in the stack, but stored in the heap.

◆ Heap: stores data generated with new

◆ Static domain: static members defined by static in objects

◆ Constant pool: stores constants.

◆ Non-ram storage: Permanent storage space such as hard disk

4. Memory instance analysis:

A. Main class:

Package YC. test;

Public class t {

Public static voidmain (string [] ARGs ){

Test test = new test ();

Test. SETI (2 );

Test. setj (2 );

System.Out. Println ("add:" + test. Add ());

}

}

B. auxiliary classes:

Package YC. test;

Public class test {

Private int I = 0;

Private Int J = 0;

Public intadd (){

Return I + J;

}

Public intgeti (){

Return I;

}

Public voidseti (int I ){

This. I = I;

}

Public intgetj (){

Return J;

}

Public voidsetj (Int J ){

This. j = J;

}

}

Analysis: Starting from the main function, ① create a reference variable test in the stack memory and set the value to NULL; ② generate an object test in the heap memory, and assign the first address value of the space to the reference variable test; ③ in the stack memory, create a basic data type variable X and assign the value to 2;

④ Assign the I (in heap memory) value of the object referred to by test to X;

⑤ After the assignment is completed, release the X variable in the stack memory before exiting the method and returning to the main function;

⑥ Repeat Step ③ ⑤ to complete the assignment of J of the object indicated by test;

7. Test calls the add method to read and retrieve the I and J Values in the heap memory for computation and complete the output.

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.