首页 > 解决方案 > 如何查看String类对象的地址?

问题描述

如上所述。例如。

String s = "abc";
System.out.println(s); // this method will output the string, not the address

那么如何查看地址,在此先感谢。

标签: javastring

解决方案


如果您想要评论中提到的答案,实际上有一种方法可以做到这一点。

String s = new String("abc");
System.out.println(Integer.toHexString(s.hashCode()));

这将返回

1ae66

这个答案在历史上被认为是正确的,并且只适用于没有覆盖 hashCode() 方法的类


推荐阅读