我的代码在这里=>
class PointHolder {
private Point point;
public PointHolder(Point point) {
this.point = point;
}
//getter
}
class Point {
private int x;
private int y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
//getter setter
}
public class Escape {
public static void main(String[] args) {
for (int i = 0; i < 20000; i++) {
test4(i);
}
}
static int foo;
public static void test4(int x) {
PointHolder pointHolder = new PointHolder(new Point(x + 2, 42));
foo = pointHolder.getPoint().getX();
}
}
使用 jitwatch,我可以看到 pointHolder 没有被分配,但new Point(x+2, 42)
仍然被分配。
我不知道为什么