大数据必学Java基础(三十四):面向对象内存分析
【摘要】 面向对象内存分析一、分析代码1 public class Person { int id; int age; public static void main(String args[]){ Person p1= new Person(); } } 内存分析二、分析代码2 public cla...
面向对象内存分析
一、分析代码1
public class Person {
int id;
int age;
public static void main(String args[]){
Person p1= new Person();
}
}
内存分析
![](https://ask.qcloudimg.com/http-save/1159019/e925875695a0bd65e836c83397dc0e76.png?imageView2/2/w/1620)
二、分析代码2
public class Person {
int id;
int age;
String school;
public Person (int a,int b,String c){
id=a;
age=b;
school=c;
}
public static void main(String args[]){
Person p= new Person(1,20, "海淀");
}
}
内存分析
![](https://ask.qcloudimg.com/http-save/1159019/6866dc3e207a19327fb20ed24454abbf.png?imageView2/2/w/1620)
三、分析代码3
class Person{
int id;
int age;
String school;
Person (int a,int b,String c){
id=a;
age=b;
school=c;
}
public void setAge(int a){
age=a;
}
}
public class Test {
public static void main(String[] args) {
Test t=new Test();
int age=40;
Person tom=new Person(1,20,"海淀");
Person jack=new Person(2,30,"朝阳");
t.change1(age);
t.change2(tom);
t.change3(jack);
System.out.println(age); //40
System.out.println("id:"+jack.id+",age:"+jack.age+",school:"+jack.school); //id:2,age:66,school:"朝阳"
}
public void change1(int i){
i=3366;
}
public void change2(Person p){
p=new Person(3,22,"西城");
}
public void change3(Person p){
p.setAge(66);
}
}
内存分析
![](https://ask.qcloudimg.com/http-save/1159019/fccceee6853de53f212346833a1eea45.png?imageView2/2/w/1620)
【声明】本内容来自华为云开发者社区博主,不代表华为云及华为云开发者社区的观点和立场。转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息,否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)