星空网 > 软件开发 > Java

面向对象编程(三)——程序执行过程中内存分析

内存分析(SxtStu.java)

栈(stack)

  存放:局部变量;

▶ 堆(heap)

  存放new出来的对象;

▶ 方法区(method)

  存放:类的信息(代码)、static变量、常量池(字符串常量)等.

栈的特点:自动分配连续的空间,后进先出;一般放置局部变量

堆的特点:不连续的空间;放置创建new出的对象;

面向对象编程(三)——程序执行过程中内存分析

下面对如下图进行分析:

面向对象编程(三)——程序执行过程中内存分析

面向对象编程(三)——程序执行过程中内存分析

具体内存分析图如下所示:

面向对象编程(三)——程序执行过程中内存分析


 

现对图2进行分析:

Student类:

public class Student {  //静态数据  String name;  int id;  int age;  int weight;    Computer computer;//每个学生有台电脑    //动态的行为  public void study(){    System.out.println(name+"在学习");  }    public void sayHello(String sname){    System.out.println(name + "向" +sname +"说,你好~");  }  }

Computer类:

public class Computer {  String brand;//品牌  int cpuSpeed;//cpu}

则:

public class Test2 {  public static void main(String[] args) {    Student s1=new Student();    s1.name="熊二";    s1.age=18;        Computer c=new Computer();    c.brand="联想";    c.cpuSpeed=100;        s1.computer =c;        //c.brand= "戴尔"; 
  System.out.println(s1.computer.brand); } }

具体内存分析如下:

面向对象编程(三)——程序执行过程中内存分析

:如果加上注释部分"c.brand= "戴尔";",s1.computer.brand最后输出的就是"戴尔".因为它们指向的是同一个地方。




原标题:面向对象编程(三)——程序执行过程中内存分析

关键词:面向对象

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

FB广告打法:https://www.goluckyvip.com/tag/14870.html
站外自然流量:https://www.goluckyvip.com/tag/14871.html
FB刷单:https://www.goluckyvip.com/tag/14872.html
免费增粉工具:https://www.goluckyvip.com/tag/14875.html
跨境站外引流:https://www.goluckyvip.com/tag/14876.html
深圳拓扑卢聪:https://www.goluckyvip.com/tag/14877.html
夹江千佛岩景区门票(夹江千佛岩景区门票价格):https://www.vstour.cn/a/411232.html
武陵山大裂谷周围景点 武陵山大裂谷周围景点图片:https://www.vstour.cn/a/411233.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流