【问题标题】:how to view the the address of object of String class? [duplicate]如何查看String类对象的地址? [复制]
【发布时间】:2020-10-27 06:46:48
【问题描述】:

如上所述。例如。

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

那么如何查看地址,提前谢谢。

【问题讨论】:

标签: java string


【解决方案1】:

Java没有“地址”的概念,所以不可能得到任何对象的地址,包括字符串。

【讨论】:

  • 谢谢。如果执行System.out.println(new int[] {1, 2, 3});,则输出[I@621be5d1。 String 类可以像那种格式输出吗?
【解决方案2】:

如果你想要你在评论中提到的答案,其实有办法做到这一点。

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

这将返回

1ae66

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

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-08-13
  • 2020-05-09
  • 2014-08-11
  • 1970-01-01
  • 2015-09-03
  • 2012-01-12
  • 1970-01-01
相关资源
最近更新 更多