A small program on String
String str1 = new String("Hello");
String str2 = "Hello";
System.out.println("=======================");
System.out.println("Srtr1 == Str2 :: " + (str1 == str2));
System.out.println("Srtr1.equals(Str2) :: " + str1.equals(str2));
Output: Srtr1 == Str2 :: false Srtr1.equals(Str2) :: true
Now how it is possible ? As we know that if equals() of two objects are true, then HashCode() of two objects must be true. But if HashCode() is true, then Equals() may be true or may not be true.
But in the above program, we see that, equals() of two String objects are true, but their hashCode() is returning false.
why it is so ???