JAVA程序执行内存分析_第1页
JAVA程序执行内存分析_第2页
JAVA程序执行内存分析_第3页
JAVA程序执行内存分析_第4页
JAVA程序执行内存分析_第5页
已阅读5页,还剩61页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、java程序执行内存分析内存分析内存中的存放code segmentdata segmentstackheap存放代码存放代码静态变量,字符串变量静态变量,字符串变量局部变量局部变量new出来的东西出来的东西birthdate类class birthdate private int day; private int month; private int year; public birthdate(int d,int m,int y) day=d;month=m;year=y; public void setday(int d)day=d; public void setmonth(int m

2、)month=m; public void setyear(int y)year=y; public int getday()return day; public int getmonth()return month; public int getyear()return year; public void dispaly()system.out.println(day+-+month+-+year);test类类public class test public static void main(string args) test test=new test(); int date=9; bi

3、rthdate d1=new birthdate(7,7,1970); birthdate d2=new birthdate(1,1,2000); test.change1(date); test.change2(d1); test.chang3(d2); system.out.println(date=+date); d1.display(); d2.display(); public void chang1(int i)i=1234; public void chang2(birthdate b)b=new birthdate(22,2,2004); public void chang3(

4、birthdate b)b.setday(22);内存中变量存放说明 test test=new test(); test为局部变量,指向new出来的对象test:xxxstackheap内存中变量存放说明 int date; 定义局部变量,占一块内存stackdate:9内存中变量存放说明 birthdate d1=new birthdate(7,7,1970); 局部变量局部变量d1,指向,指向new出来的对象出来的对象stackd1:xxxy1970m7d7heapdaymonthyear构造方法调用结束后,构造方法调用结束后,d,m,y将被释放将被释放内存中变量存放说明 test.ch

5、ange1(date); 调用调用test中的中的change1方法方法stackdate:9ii:9i:1234传值传值赋值赋值调用结束,调用结束,i被释放被释放内存中变量存放说明 test.change2(d1);heapstackd1:xxxb:xxxday:7month:7year:1970传地址传地址new:xxxday:22month:2year:2004传地址传地址123456方法调用结束后方法调用结束后b和和new消失消失7内存中变量存放说明 test.chang3(d2);stackheapd2:xxxb:xxxday:1month:1year:2000d:221324传值传

6、值传值传值方法调用结束后方法调用结束后b和和d消失消失5三维坐标中的点 class point int x,y,z; point(int a,int b,int c) x=a;y=b;z=c; void getdistance(point p) return (x-p.x)*(x-p.x)+(y-p.y)*(y-p.y)+(z-p.z)*(z-p.z); point p1=new point(1,2,3); system.out.println(+getdistance(new point(0,0,0);上例内存分析 system.out.println(+getdistance(new po

7、int(0,0,0);stackheapx:1y:2z:3x:0y:0z:0p1:xxxp:xxx132传地址传地址调用结束后调用结束后p被释放被释放this关键字的引用关键字的引用 public class leaf int i=0; leaf(int i)this.i=i; leaf increament() i+;return this; void print()system.out.println(i=+i); public static void main(string args) leaf leaf=new leaf(100); leaf.increament().increame

8、nt().print(); this关键字的引用关键字的引用 leaf leaf=new leaf(100);stackheapthisi:100i:100调用结束后调用结束后i被释放被释放leaf:xxx想象想象this在类中在类中this关键字的引用关键字的引用 leaf.increament().increament().print();leaf:xxxthisi:102leaf1:xxxleaf2:xxxthis是当前对象的是当前对象的引用引用stackheapthis关键字的引用关键字的引用 leaf.increament().increament().print(); 相当于以下程

9、序段:相当于以下程序段: leaf.increament(); leaf.increament();leaf.print(); leaf.increament().increament().print();返回返回leaf对象的对象的引用引用l1,指向,指向leaf对象对象返回返回leaf对象的对象的引用引用l2,指向,指向leaf对象对象内存分析(一) class pointxy /分析程序在内存中执行过分析程序在内存中执行过程程private double x;private double y;pointxy(double x1,double y1)x=x1;y=y1;public dou

10、ble getx()return x;public double gety()return y;public void setx(double i)x=i;public void sety(double i)y=i; 内存分析(二)class circleprivate pointxy o;private double radius;circle(pointxy p,double r)o=p;radius=r;circle(double r)o=new pointxy(0.0,0.0);radius=r;boolean contains(pointxy p)double x=p.getx()-

11、o.getx();double y=p.gety()-o.gety();if(x*x+y*yradius*radius) return false;else return true;public void seto(double x,double y)o.setx(x);o.sety(y);public pointxy geto()return o;public double getradius()return radius;public void setradius(double r)radius=r;public double area()return 3.14*radius*radius

12、;内存分析(三)public class testcircle public static void main(string args)circle c1=new circle(new pointxy(1.0,2.0),2.0); circle c2=new circle(5.0);system.out.println(c1:(+c1.geto().getx()+,+c1.geto().gety()+),+c1.getradius();system.out.println(c2:(+c2.geto().getx()+,+c2.geto().gety()+),+c1.getradius();sy

13、stem.out.println(c1 area =+c1.area();system.out.println(c2 area =+c2.area();c1.seto(5, 6);c2.setradius(9.0);system.out.println(c1=(+c1.geto().getx()+,+c1.geto().gety()+),+c1.getradius();system.out.println(c2=(+c1.geto().getx()+,+c2.geto().gety()+),+c2.getradius();system.out.println(c1 area =+c1.area

14、();system.out.println(c2 area =+c2.area();pointxy p1=new pointxy(5.2,6.3);system.out.println(c1.contains(p1);system.out.println(c1.contains(new pointxy(10.0,9.0);circle c1=new circle(new pointxy(1.0,2.0),2.0);c1:xxxx:1.0y:2.0 x1:1.0y1:2.0oradius:2.0p:xxxr:2.0紫色的调用结束后释放紫色的调用结束后释放红色的几位调用结束后内存布局红色的几位调用

15、结束后内存布局stackheapcircle c2=new circle(5.0);c2:xxxoradius:5.0r:5.0 x1:0.0y1:0.0 x:0.0y:o.ostackheapsystem.out.println(c1:(+c1.geto().getx()+,+c1.geto().gety()+),+c1.getradius();c1:xxxoradius:2.0 x:1.0y:2.0和和o里的值一样里的值一样1.0返回值返回值返回值返回值stackheap2.0返回值返回值c1.seto(5, 6);c2.setradius(9.0);oradius:2.0oradius:

16、5.09.0 x:1.05y:2.06x:0.0y:0.0c1:xxxc2:xxxx:5y:6i:5i:6r:9.0heapstacksystem.out.println(c1.contains(p1);oradius:2.0 x:5y:6x:5.2y:6.3c1:xxxp1:xxxp:xxxxystackheaptruestatic关键字的使用public class cat private static int sid=0; private string name; int id; cat(string name) =name; id=sid+; public void

17、info system.out.println(my name is +name+ no.+id); public static void main(string args) cat.sid=100; cat.mimi=new cat(mimi); cat.pipi=new cat(pipi); (); (); static关键字的使用stackheapdata segsid:100101102id:100nameid:101namemimi:xxxpipi:xxxmimipipinamename类的继承类的继承studentpersonnameagesch

18、oolstackheap子类子类父类父类java权限修饰符修饰符修饰符类内部类内部同一个包同一个包 子类子类任何地方任何地方privateyesdefaultyesyesprotectcdyesyesyespublicyesyesyesyessuper和this的使用cc:xxxvaleuvaluethissuper子类子类父类父类testsupr.class stu s1=new stu(c,s1);persnamelocationschoolthissuperheapbeijingcs1s1nameschoolnlschoolnamelocationdata segstackhashcod

19、es tablehashcodes table对象对象对象对象对象对象对象对象对象对象每一个对象都有独一无二的每一个对象都有独一无二的hashcodes,通过它可以找到所在对象的位置通过它可以找到所在对象的位置对象转型对象转型casting(testanimals) class animal class dg extends animal animal a=new animal();/ a=new dg();/ dg d1=(dg)a;/对象转型对象转型casting(testanimals)animal aanimalnamefurcolor程序中把程序中把a当成一只普通的当成一只普通的an

20、imal,不能访问,不能访问dg中的成员中的成员dg不能访问不能访问表明表明实质实质只能访问作为父类的那部分成员只能访问作为父类的那部分成员dg d1动态绑定和多态(testpolymoph)cate cname:catnameeyecolor:bluename:l1anima petlady l1实质上指向实质上指向code seganima enjoycate enjoydo enjoy根据实际对象类型来调用根据实际对象类型来调用方法方法星际的多态应用父类父类gameobjectdraw()继承继承使用多态使用多态异常的概念异常的概念 public void somemethod() th

21、rows someexception if(somecondition() throw new someexception(错误原因); 声明该方法可能抛出的异常声明该方法可能抛出的异常构造并抛出异常对象构造并抛出异常对象抛出异常抛出异常 try somemethod(); catch(someexception e) /异常处理代码; 调用该方法时试图捕捉异常调用该方法时试图捕捉异常定义处理异常的代码定义处理异常的代码异常的分类throwableerrorexceptionruntimeexception可抛出的可抛出的系统错误,处理不了系统错误,处理不了能处理的错误能处理的错误经常出的错误

22、,可以不经常出的错误,可以不catch必须得必须得catch的异常的异常运行时异常运行时异常非运行时异常非运行时异常method() throws someexception.包含包含异常类的父类异常类的父类异常的捕获和处理异常的捕获和处理 try /可能抛出异常的语可能抛出异常的语 catch(someexception e1) . catch(someexception e2) . finally.统一的出口,都要被执行,可进行资源的清除工作统一的出口,都要被执行,可进行资源的清除工作 readfile() throws ioexception. method1() throws ioex

23、ception readfile() method2() throws ioexception method1() public static main(string s) try method2() catch(ioexception e). 抛异常抛异常抛异常抛异常抛异常抛异常捕获捕获处理处理main方法可不处理,向外抛方法可不处理,向外抛 int s; 数组定义数组定义 s=new int5;xxxsstackheap自动初始化都为自动初始化都为0 date days; 数组分配空间和赋值数组分配空间和赋值 days=new date3; for(int i=0;i3;i+) daysi

24、=new date(2004,4,i+1);xxxxxxxxxxxx200441200442200443daysstackheap数组动态初始化数组动态初始化 int a;/定义定义 a=new int3;/分配空间分配空间 a0=1;a1=2;a2=3;/赋值赋值 date days;/定义定义 days=new date2;/分配空间分配空间 days0=new date(2004,4,1);/赋值赋值 days1=new date(2004,4,2);/赋值赋值数组定义与为数组元素分配空间和赋值分开进行数组定义与为数组元素分配空间和赋值分开进行数组元素静态初始化 int a3=1,2,3

25、; date days= new date(2004,4,1), new date(2004,4,2), new date(2004,4,3) ;在定义数组的同时就分配空间和赋值在定义数组的同时就分配空间和赋值数组元素的默认初始化数组元素的默认初始化 数组是引用类型,它的元素相当于类的成员变量,因此数组分配空间后,每个元素也被按照成员变量的规则被隐式初始化 int a=new int3; system.out.println(a1); date days=new date3; system.out.prinyln(days2);数组作为返回值.date dxxxx返回值返回值数组模拟链表数组模

26、拟链表1234050154323450202345删除该元素删除该元素二维数组xxxx a0a2a00a01a10a11a20a21.heapstacka1xxxxxxxxxxxxa一维数组一维数组数组的数组数组的数组java中多维数组的声明和初始化应按从高维到低维的顺序进行 int a=new int3; /动态初始化 a0=new int2; a1=new int4; a2=new int3; int a=new int35; /动态初始化 int a=4,2,13,32,424,32,1,32,12; /静态初始化 二维数组的访问:aij二维数组的输出 int a=1,2,3,4,5,6

27、,7,8,9; for(int i=0;ia.length;i+) for(int j=0;jai.length;j+) system.out.print (a+i+j+=+aij+,); system.out.println(); 该循环可用于二维数组的整体赋值和输出二维字符串数组数组元素数组元素一维一维字符串字符串二维二维string类不可变 string s1=hello; string s2=world; s1+=s2;helloworldhelloworlds1s2新建的对象不可变不可变容器apicollection set list map hashset linkedlist a

28、rraylist hashmap无顺序,不可重复有顺序,可重复(equals)一个一个向里装一个一个向里装一对一对向里装一对一对向里装iterator容器容器 next 元素元素游标游标array与与linked比较比较去掉去掉arraylinked改时要建一个新的数组读时沿着链表向下查找inputstream流流pipedinputstreamfilterinputstreambytearrayinputstreamsequenceinputstreamstringbufferinputstreamobjectinputstreamlinenumberinputstreamdatainput

29、streambufferedinputstreampushbackinputstreamfileinputstreaminputstream节点流节点流处理流处理流outputstream类类fileoutputstreampipedoutputstreamfilteroutputstreambytearrayoutputstreamobjectoutputstreamdataoutputstreambufferedoutputstreamprintstreamoutputstream处理流处理流节点流节点流节点流类型节点流类型 类型类型 字符流字符流 字节流字节流file(文件)filereader filewriter fileinputstream fileoutputstreammemory arraaychararrayreaderchararraywriterbytearrayinputstbytearrayoutputstmemory stringstringreaderstringwriter pipe(管道)pipedreaderpipedwriterpipedinputstreampipedoutp

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论