Any way to see HashCode of primitive types?

别等时光非礼了梦想. 提交于 2019-12-10 14:26:40

问题


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

error: int cannot be dereferenced

Does this means that if int n = 10 then its HashCode will also be 10??

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);
     }

回答1:


You cannot invoke methods on primitive types.

n is declared as int. It does not have methods.

It does not make sense to think like

If i still need to see hascode for int in below program

You could create an Integer object and get its hashCode()

Integer.valueOf(n).hashCode()

Integer#hashCode() is implemented as

public int hashCode() {
    return value;
}

where value is the int value it's wrapping.

Does this means that if int n = 10 then its HashCode will also be 10??

The int doesn't have a hashcode. Its Integer wrapper will however have the value of the int it's wrapping.




回答2:


Only Objects have methods. Not primitives. You might want

 int h2 = new Integer(n).hashCode();

Just create an Wrapper of int and invoke method on it.




回答3:


Simple answer is int is not a Java object and there is not such thing call hashCode() for int.




回答4:


New Convenience Methods (Java 8)

Java 8 brought static methods to more conveniently generate a hash code for primitive values. These methods are stored on the primitive equivalent class.

Mentioned in this discussion with bondolo (Mike Duigou?), one of the OpenJDK developers, talking about how Google Guava has influenced the core libraries in Java.

In Java 8 examples of small Guava inspired JDK additions include static hashCode methods added to the primitive types.

  • Boolean.hashCode
  • Integer.hashCode
  • Float.hashCode

…and so on.




回答5:


int is not an Object. It doesn't have methods.

If you want some idea of how the Integer class works, you can Grep Oracle's Java code: http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/7-b147/java/lang/Integer.java#Integer.hashCode%28%29

In particular, this confirms that the return of hashCode() for an Integer object is indeed its underlying integer value.




回答6:


int is not an object in Java. It is a primitive data type. However, you can use Integer class to get the hashcode of an int.

System.out.println(new Integer(n).hashCode());

Hope this helps.




回答7:


It does not make sense for primitives to have hash codes. They themselves are the best hash codes you can ask for.

Integer hash code is implemented to redundantly return the value itself, so that when used in a java.util.HashSet for example, it maps to itself.

public int hashCode() {
    return value;
}


来源:https://stackoverflow.com/questions/21748355/any-way-to-see-hashcode-of-primitive-types

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!