概念:(首先弄清楚几个名词)
对象:(存放在堆内存中)
new Person(); //Person对象
对象变量:(存放在栈内存中)
Person obj; //对象变量
内存分配:
1.
Person person = null; //在栈中分配空间
2.
person = new Person("John", 20); //在堆中分配空间并赋初值
3.
Person person2 = new Person("Lily",21); //为person2分配栈和堆空间
4.
person = person2; //person2 的引用指向了person的堆内存
5.
堆内存的空间:.不再存在指向person("Lily",21)的引用,因此在一定时间内GC将回收该空间;