有什么办法可以看到原始类型的HashCode吗? [英] Any way to see HashCode of primitive types?

查看:98
本文介绍了有什么办法可以看到原始类型的HashCode吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

搞砸基本D.S ....我读到,没有用于原始类型inthashCode()方法,如果在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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆