Example of ==, equals and hashcode in java

Posted by Abhishek Jain on Stack Overflow See other posts from Stack Overflow or by Abhishek Jain
Published on 2010-04-28T17:47:21Z Indexed on 2010/04/28 17:57 UTC
Read the original article Hit count: 307

Filed under:
|
|

Given this:

String s1= new String("abc");
String s2= new String("abc");
String s3 ="abc";
System.out.println(s1==s3);
System.out.println(s1==s2);
System.out.println(s1.equals(s2));
System.out.println(s1.equals(s3));
System.out.println(s1.hashCode());
System.out.println(s2.hashCode());
System.out.println(s3.hashCode());

Output is: false false true true 96354 96354 96354

Here == is giving false for each object but hashcode for each String object is same. Why is it so?

© Stack Overflow or respective owner

Related posts about java

Related posts about hashcode