gpt4 book ai didi

java - 类地址测试

转载 作者:行者123 更新时间:2023-12-01 18:10:31 25 4
gpt4 key购买 nike

当我一次又一次运行 Java 代码时,它会打印 p 地址的不同值。这是为什么?

点代码:

class Point {

}

主要代码:

public class HelloWorld
{
public static void main(String[] args) {
Point p = new Point();
System.out.println(p);
p = null;
System.gc();
}
}

控制台日志:

test.Point@44585f2a
test.Point@5cfe174
test.Point@44585f2a
test.Point@44585f2a
test.Point@5cfe174
test.Point@44585f2a

最佳答案

打印出 getClass().getName() + '@' + Integer.toHexString(hashCode()),而不是地址。 Reference

还有,为什么每次启动程序时地址都是一样的?任何其他进程都可能在程序执行之间占用该位置。

此外,我看到您正在调用 System.gc (); 小心!它并不总是按照人们期望的那样做! Reference

关于java - 类地址测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33406493/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com