what is the difference between == operator and equals()? (with hashcode() ???)

后端 未结 6 743
长发绾君心
长发绾君心 2020-11-28 15:20

I was learning hashcode in more depth and figured that:

1. If you override equals(), you must override hashcode() too.

2. To find if

6条回答
  •  感动是毒
    2020-11-28 15:56

    Most is already answered, so here's just another enlightening example:

    String s1 = "foo";
    String s2 = "foo";
    System.out.println(s1 == s2); // true, because same reference (string pool)
    
    String s3 = new String("foo");
    String s4 = new String("foo");
    System.out.println(s3 == s4); // false, because different reference
    System.out.println(s3.equals(s4)); // true, because same value
    

提交回复
热议问题