Memory Analysis (J2SE Topic III)

Source: Internet
Author: User
Memory Analysis

1. The types of variables in the Java language, except for the basic types, are called application types. Objects in Java are manipulated by reference.

2. The distinction between classes and objects in memory:

A, class is a static concept, stored in the code area;

B, the object is new out, in the heap memory (dynamic), the class of each member variable in different objects have different values (in addition to static variables), and the method is only one, the execution of the time to occupy memory.

3, Java in memory allocation of areas involved:

Registers: We have no control in the program

Stacks: A reference to the underlying type of data and objects, but the object itself is stored in the heap instead of being stored on the stack

Heap: Storing data produced with new

Static fields: Static members that are stored in the object with static definitions

Constant pools: Storing constants

Non-RAM storage: Permanent storage space such as hard disk

4, memory example analysis:

A, main class:

Package yc.test;
public class T {public
	static void Main (string[] args) {
		test test = new test ();
		Test.seti (2);
		TEST.SETJ (2);
		System.out.println ("Add:" +test.add ());
	}

B, Auxiliary class:

Package yc.test;
public class Test {
	private int i = 0;
	private int j = 0;
	public int Add () {return
		i+j;
	}
	public int Geti () {return
		i;
	}
	public void SetI (int x) {
		this.i = x;
	}
	public int Getj () {return
		J;
	}
	public void setj (int y) {
		this.j = y;
	}
}

Analysis: Starting from the main function,

① in stack memory, create a reference variable test and set the value to null;

② in heap memory, generates an object test and assigns its space address value to the reference variable test;

③ in stack memory, create a basic data type variable x and assign a value of 2;

④ assigns the I (heap memory) of the object in test to X;

After the ⑤ completes the assignment, the x variable in the stack memory is released before the exit method returns to the main function.

⑥ Repeat ③④⑤ step to complete the assignment of J of the object referred to in test;

⑦test calls the Add method to read the value of I and J in the heap memory and completes 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.