一个对象的内存图
package cn.hu.day01.demo03;
public class Phone {
String brand;//品牌
double price;
String color;
public void call(String who){
System.out.println("给"+who+"打电话");
}
public void sendMessage(){
System.out.println("群发短信");
}
}
package cn.hu.day01.demo03;
public class Demo01PhoneOne {
public static void main(String[] args) {
Phone one=new Phone();
System.out.println(one.brand);//null
System.out.println(one.price);//0.0
System.out.println(one.color);//null
System.out.println("==========================");
one.price =8388.0;
one.color ="黑色";
System.out.println(one.brand);
System.out.println(one.price);
System.out.println(one.color);
System.out.println("==========================");
one.call("乔布斯");
one.sendMessage();
}
}
过程:
1.将两个类的定义(成员变量和成员方法)存入方法区
2.程序先从main方法开始,所以main方法先在栈区压栈并开辟空间
3.执行main方法中代码 Phone one 局部变量创建在栈区,并开辟小段空间来存地址
3. 执行main方法中代码new Phone();在堆区创建对象开辟空间
4. 对成员变量赋初值成员变量所在空间有其地址0x666
5. 成员方法在堆区只开辟存放地址的小空间,其真正的位置在方法区
6. 执行代码 one.brand =“苹果”,先在栈区中找到Phone one空间存放的对象地址
7. 然后找到堆区开辟出来的空间,将brand的值由初始化的null改为"苹果"
8.调用成员方法 one.call(“乔布斯”) 时先在Phone one空间找到0x666然后找到堆区的0x333
9.根据0x333找到方法区的成员方法call(String who)
10.然后该成员方法在栈区压栈调用,该方法执行完后立即弹出栈区,轮到main方法继续执行
11.当main方法执行完后,main方法也弹出栈。程序执行完毕
两个对象使用同一方法的内存图
package cn.hu.day01.demo03;
public class Phone {
String brand;//品牌
double price;
String color;
public void call(String who){
System.out.println("给"+who+"打电话");
}
public void sendMessage(){
System.out.println("群发短信");
}
}
package cn.hu.day01.demo03;
public class Demo01PhoneTwo {
public static void main(String[] args) {
Phone one=new Phone();
System.out.println(one.brand);//null
System.out.println(one.price);//0.0
System.out.println(one.color);//null
System.out.println("==========================");
one.brand ="苹果";
one.price =8388.0;
one.color ="黑色";
System.out.println(one.brand);
System.out.println(one.price);
System.out.println(one.color);
System.out.println("==========================");
one.call("乔布斯");
one.sendMessage();
System.out.println("==========================");
Phone two=new Phone();
System.out.println(two.brand);//null
System.out.println(two.price);//0.0
System.out.println(two.color);//null
System.out.println("==========================");
two.brand ="三星";
two.price =5999.0;
two.color ="蓝色";
System.out.println(two.brand);
System.out.println(two.price);
System.out.println(two.color);
System.out.println("==========================");
two.call("欧巴");
two.sendMessage();
}
}
两个引用指向同一个对象
package cn.hu.day01.demo03;
public class Phone {
String brand;//品牌
double price;
String color;
public void call(String who){
System.out.println("给"+who+"打电话");
}
public void sendMessage(){
System.out.println("群发短信");
}
}
package cn.hu.day01.demo03;
public class Demo01PhoneSame {
public static void main(String[] args) {
Phone one=new Phone();
System.out.println(one.brand);//null
System.out.println(one.price);//0.0
System.out.println(one.color);//null
System.out.println("==========================");
one.brand ="苹果";
one.price =8388.0;
one.color ="黑色";
System.out.println(one.brand);
System.out.println(one.price);
System.out.println(one.color);
System.out.println("==========================");
one.call("乔布斯");
one.sendMessage();
System.out.println("==========================");
//将one当中保存的对象地址值赋值给two
Phone two=one;
System.out.println(two.brand);//null
System.out.println(two.price);//0.0
System.out.println(two.color);//null
System.out.println("==========================");
two.brand ="三星";
two.price =5999.0;
two.color ="蓝色";
System.out.println(two.brand);
System.out.println(two.price);
System.out.println(two.color);
System.out.println("==========================");
two.call("欧巴");
two.sendMessage();
}
}