有什么办法可以看到原始类型的HashCode吗? [英] Any way to see HashCode of primitive types?
问题描述
搞砸基本D.S ....我读到,没有用于原始类型int
的hashCode()
方法,如果在int
上调用,它将抛出错误
Mugging up basic D.S....I read that, no hashCode()
method for primitive type int
is available and if called on int
, it will throw error
错误:无法取消引用int
error: int cannot be dereferenced
这是否意味着如果int n = 10
,则其HashCode
也将为10?
Does this means that if int n = 10
then its HashCode
will also be 10??
如果我仍然需要在下面的程序中查看int
的hascode,是否有一种方法可以查看它,例如Integer
Wrapper
???
If i still need to see hascode for int
in below program, is there a method to see it, like Integer
Wrapper
???
public static void main(String []args){
String me = "hello";
int n = 10;
int h1 = me.hashCode();
int h2 = n.hashCode();
System.out.println(h1);
System.out.println(h2);
}
推荐答案
您不能在原始类型上调用方法.
You cannot invoke methods on primitive types.
n
被声明为int
.它没有方法.
n
is declared as int
. It does not have methods.
像
如果我仍然需要在下面的程序中查看int的hascode
If i still need to see hascode for int in below program
您可以创建一个Integer
对象并获取其hashCode()
You could create an Integer
object and get its hashCode()
Integer.valueOf(n).hashCode()
Integer#hashCode()
的实现方式为
public int hashCode() {
return value;
}
其中value
是它要包装的int
值.
这是否意味着如果int n = 10,则其HashCode也将为10?
Does this means that if int n = 10 then its HashCode will also be 10??
int
没有哈希码.但是,其Integer
包装器将具有其包装的int
的值.
The int
doesn't have a hashcode. Its Integer
wrapper will however have the value of the int
it's wrapping.
这篇关于有什么办法可以看到原始类型的HashCode吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!