文章目录
代码
- Student类:
public class Student {
int no;
String name;
int age;
boolean sex;
String address;
}
- StudentTest类:
public class StudentTest {
public static void main(String[] args) {
Student s1 = new Student();
Student s2 = new Student();
}
}
JVM中对应内存结构图
note:
- s1、s2是局部变量(在方法中创建的变量),局部变量存储在栈内存中
- 对象和引用的区别?
- 对象是通过new出来的,在堆内存中存储
- 引用是:但凡是变量,并且该变量中保存了内存地址,指向了堆内存当中的对象(此例中main方法中的变量s1、s2就是保存了对象Student在堆内存中的地址)