This question already has an answer here:
I am not new to java Reflection
. I have tried this Program just as a experiment but Suddenly I surprised with the output what I Get.
Here is my Student
Class.
Student.java
class Student{
String name;
int marks;
public Student(String name){
this.name = name;
}
public String getName(){
return name;
}
public int getMarks(){
return 10;
}
}
Here is my Test
class . in Which s1
and s2
both are different Objects of Class Student
.
Test.java
import java.lang.reflect.*;
public class Test{
public static void main(String... args){
Student s1 = new Student("Vikrant"); // Creating First Object
Class c1 = s1.getClass();
Student s2 = new Student("Vikash"); // Creating Second Object
Class c2 = s2.getClass();
System.out.println(c1.hashCode()); // Output 654391685
System.out.println(c2.hashCode()); // Output 654391685 why same HashCode ??
System.out.println(s1==s2); //expecting false and actual is false.
System.out.println(c1==c2); //expecting false buttt get True as a Result. Why ????
}
}
Now My Question is
-
why
System.out.println(c1.hashCode());
andSystem.out.println(c2.hashCode());
having same Hash Code ??? -
System.out.println(s1==s2);
provides Output as expectedfalse
butSystem.out.println(c1==c2);
Output istrue
notfalse
??
While both c1
and c2
referenced to two different object of Student
.
Aucun commentaire:
Enregistrer un commentaire