我有一个下面给出的课程。在这个类中,哪些变量将存储在堆中,哪些变量将存储在 java 内存模型的线程堆栈中。我知道局部变量存储在堆栈中,而对象存储在堆中。但我需要给定班级的解释。
class School {
public int id =1;
public Student student=new Student();;
public void method(Student st, int f ){
Student stud = new Student();
int fees = 100;
}
}
谢谢
Student stud
是对对象的引用,而不是对象。引用在栈上,对象在堆上。
Student stud = new Student();
^ ^
reference on stack object on heap
注意:使用 Escape Analysis,也可以将对象放在堆栈上,尽管这是一种您通常不需要知道正在发生的优化。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句