The java.lang.reflect.Method.hashCode() method returns the hash code for the Method class object. The hashcode returned is computed by exclusive-or operation on the hashcodes for the method’s declaring class name and the method’s name. The hashcode is always the same if the object doesn’t change. Hashcode is a unique code generated by the JVM at time of object creation. It can be used to perform some operation on hashing related algorithms like hashtable, hashmap etc. An object can also be searched with this unique code.
Syntax:
public int hashCode()
Returns: It returns an integer value which represents hashCode value for this Method.
Example:
Method: public void getvalue(){} HashCode: 1553975225 Explanation: Hashcode is a unique code generated by the JVM at time of creation of the object of Method getValue.when we going to apply hashCode function on method object of getValue it will return 1553975225 as hashCode. Method:public void paint(){} HashCode: 1643975341
Below program illustrates hashcode() method of Method class:
Program 1: Get the hash code of a specific method object created by calling getDeclaredMethod() of Class object.
Tóm Tắt
Java
import
java.lang.reflect.Method;
public
class
GFG {
public
void
getSampleMethod() {}
public
static
void
main(String args[])
{
try
{
Class c = GFG.
class
;
Method method = c.getDeclaredMethod("getSampleMethod",
null
);
int
hashCode = method.hashCode();
System.out.println("hashCode of method " + method.getName()
+ " is " + hashCode);
}
catch
(Exception e) {
e.printStackTrace();
}
}
}
Output:
hashCode of method getSampleMethod is 1553813225
Program 2:
In this program, after getting a list of Method objects of a class object by calling getMethods() method of class object, hashCode() method of Method object is called for each method object of the list. At last, the hashcode is printed along with the method name.
Java
import
java.lang.reflect.Method;
public
class
GFG {
public
void
getSampleMethod() {}
public
String setSampleMethod()
{
String str = "hello India";
return
str;
}
public
static
void
main(String args[])
{
try
{
Class c = GFG.
class
;
Method[] methods = c.getMethods();
for
(Method m : methods) {
int
hashCode = m.hashCode();
System.out.println("hashCode of method "
+ m.getName()
+ " is " + hashCode);
}
}
catch
(Exception e) {
e.printStackTrace();
}
}
}
Output:
hashCode of method main is 3282673 hashCode of method getSampleMethod is 1553813225 hashCode of method setSampleMethod is -1830532123 hashCode of method wait is 1063184614 hashCode of method wait is 1063184614 hashCode of method wait is 1063184614 hashCode of method equals is -1918826964 hashCode of method toString is -1451283457 hashCode of method hashCode is 933549448 hashCode of method getClass is 1261057617 hashCode of method notify is -43061542 hashCode of method notifyAll is 1312178187
Explanation: Output of this program also showing results for method objects other than methods defined in class object like wait, equals, toString, hashCode, getClass, notify, and notifyAll, which are inherited from superclass Object of java.lang package by class Object.
Reference:
https://docs.oracle.com/javase/8/docs/api/java/lang/reflect/Method.html#hashCode–
My Personal Notes
arrow_drop_up