Object-Oriented Programming (III)-memory analysis during program execution and Object-Oriented Programming

Source: Internet
Author: User

Object-Oriented Programming (III)-memory analysis during program execution and Object-Oriented Programming
Memory analysis (SxtStu. java)

Stack):

Storage: local variables;

 Heap):

Stores new objects;

 Method):

Storage: class information (CODE), static variables, constant pools (string constants), etc.

Stack features: automatic allocation of continuous space, followed by first-in-first-out; generally placed with local variables

Heap features: discontinuous space; placement of newly created objects;

Perform the following analysis:

The memory analysis diagram is as follows:

 

Now we analyze Figure 2:

Student class:

Public class Student {// static data String name; int id; int age; int weight; Computer computer; // Each Student has a Computer // dynamic behavior public void study () {System. out. println (name + "");} public void sayHello (String sname) {System. out. println (name + "" + sname + ", Hello ~ ");}}

Computer Class:

Public class Computer {String brand; // brand int cpuSpeed; // cpu}

Then:

Public class Test2 {public static void main (String [] args) {Student s1 = new Student (); s1.name = "Xiong 'er"; s1.age = 18; computer c = new Computer (); c. brand = "Lenovo"; c. cpuspeed= 100; s1.computer = c; // c. brand = "Dell ";
  System.out.println(s1.computer.brand); } }

The memory analysis is as follows:

Note: If you add the comment section "c. brand =" Dell ";", s1.computer. brand finally outputs "Dell" because they point to the same place.

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.