1 package exercise; 2 3 class Tank 4 { 5 int level; 6 } 7 8 public class Assignment{ 9 public static void main(String[]args){ 10 Tank t1=new Tank(); 11 Tank t2=new Tank(); 12 13 t1.level=9; 14 t2.level=47; 15 16 System.out.println("t1.level:"+t1.level+",t2.level:"+t2.level); 17 18 t1=t2; 19 System.out.println("t1.level:"+t1.level+",t2.level:"+t2.level); 20 //when you assign "from one object to another" ,your are actually copying a reference 21 //from one place to another. 22 //this means that if you say c=d for objects,you end up with both c and d pointing to 23 //the object that, originally,only d pointed to . 24 25 t1.level=27; 26 System.out.println("t1.level:"+t1.level+",t2.level:"+t2.level); 27 } 28 }
输出
1 t1.level:9,t2.level:47 2 t1.level:47,t2.level:47 3 t1.level:27,t2.level:27