public class abc1 {
private String s;
public abc1(String s){this.s=s;}
public static void main(String args[])
{
HashSet<Object> hs=new HashSet<Object>();
abc1 a1= new abc1("abc");
abc1 a2= new abc1("abc");
String s1= new String("abc");
String s2= new String("abc");
hs.add(a1);
hs.add(a2);
hs.add(s1);
hs.add(s2);
System.out.println(hs.size());
}
}
Why above program output is 3?
Edit
Seeing below comments I am extending my question:
System.out.println (s1 == s2);
Are s1 and s2 refering to same object? If then the above statement should print true but its output is false.
Are they are similar in terms of hashcode but still differnt?