一个对象的内存图
public class Phone { String brand; //品牌 double price; //价格 String color; //颜色 public void call(String who) { System.out.println("给" + who + "打电话"); } public void sendMessage() { System.out.println("群发短信"); } } 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 one.brand = "苹果"; one.price = 8388.0; one.color = "黑色"; System.out.println(one.brand); //苹果 System.out.println(one.price); //8388.0 System.out.println(one.color); //黑色 one.call("乔布斯"); //给乔布斯打电话 one.sendMessage(); //群发短信 } } D:\Java\jdk-17\bin\java.exe "-javaagent:D:\BaiduNetdiskDownload\IntelliJ IDEA 2022.2.3\lib\idea_rt.jar=9084:D:\BaiduNetdiskDownload\IntelliJ IDEA 2022.2.3\bin" -Dfile.encoding=UTF-8 -classpath F:\IdeaProjects\JavaSenior\out\production\variableandoperator Demo01PhoneOne null 0.0 null 苹果 8388.0 黑色 给乔布斯打电话 群发短信 Process finished with exit code 0
两个对象使用同一个方法的内存图
public class Phone { String brand; //品牌 double price; //价格 String color; //颜色 public void call(String who) { System.out.println("给" + who + "打电话"); } public void sendMessage() { System.out.println("群发短信"); } } class Demo02PhoneTwo { 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 one.brand = "苹果"; one.price = 8388.0; one.color = "黑色"; System.out.println(one.brand); //苹果 System.out.println(one.price); //8388.0 System.out.println(one.color); //黑色 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 two.brand = "三星"; two.price = 5999.0; two.color = "蓝色"; System.out.println(two.brand); //三星 System.out.println(two.price); //5999.0 System.out.println(two.color); //蓝色 two.call("欧巴"); //给欧巴打电话 two.sendMessage(); //群发短信 } } D:\Java\jdk-17\bin\java.exe "-javaagent:D:\BaiduNetdiskDownload\IntelliJ IDEA 2022.2.3\lib\idea_rt.jar=10771:D:\BaiduNetdiskDownload\IntelliJ IDEA 2022.2.3\bin" -Dfile.encoding=UTF-8 -classpath F:\IdeaProjects\JavaSenior\out\production\variableandoperator Demo02PhoneTwo null 0.0 null 苹果 8388.0 黑色 给乔布斯打电话 群发短信 ================== null 0.0 null 三星 5999.0 蓝色 给欧巴打电话 群发短信 Process finished with exit code 0
两个引用指向同一个对象的内存图
public class Phone { String brand; //品牌 double price; //价格 String color; //颜色 public void call(String who) { System.out.println("给" + who + "打电话"); } public void sendMessage() { System.out.println("群发短信"); } } class Demo03PhoneSame { 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 one.brand = "苹果"; one.price = 8388.0; one.color = "黑色"; System.out.println(one.brand); //苹果 System.out.println(one.price); //8388.0 System.out.println(one.color); //黑色 one.call("乔布斯"); //给乔布斯打电话 one.sendMessage(); //群发短信 System.out.println("=================="); Phone two = one; System.out.println(two.brand); //苹果 System.out.println(two.price); //8388.0 System.out.println(two.color); //黑色 two.brand = "三星"; two.price = 5999.0; two.color = "蓝色"; System.out.println(two.brand); //三星 System.out.println(two.price); //5999.0 System.out.println(two.color); //蓝色 two.call("欧巴"); //给欧巴打电话 two.sendMessage(); //群发短信 } } D:\Java\jdk-17\bin\java.exe "-javaagent:D:\BaiduNetdiskDownload\IntelliJ IDEA 2022.2.3\lib\idea_rt.jar=11799:D:\BaiduNetdiskDownload\IntelliJ IDEA 2022.2.3\bin" -Dfile.encoding=UTF-8 -classpath F:\IdeaProjects\JavaSenior\out\production\variableandoperator Demo03PhoneSame null 0.0 null 苹果 8388.0 黑色 给乔布斯打电话 群发短信 ================== 苹果 8388.0 黑色 三星 5999.0 蓝色 给欧巴打电话 群发短信 Process finished with exit code 0