字符串的哈希码

Bis*_*ury 1 java string object

当我们尝试使用toString()HashCode 打印任何对象时(如果toString()没有覆盖).但是,如果我想打印字符串变量的Hashcode,我该怎么办.这个问题与Java有关.

Jes*_*per 8

只需调用hashCode()String对象:

String s = "Hello World";
System.out.println(s.hashCode());
Run Code Online (Sandbox Code Playgroud)

如果你想要它的格式相同Object.toString(),试试这个:

System.out.println(Integer.toHexString(s.hashCode()));
Run Code Online (Sandbox Code Playgroud)

  • 更确切地说:`s.getClass().getName()+"@"+ Integer.toHexString(s.hashCode());` (2认同)