what is the difference in s2 and s3 declaration?
here hashcode value of s1,s2,s3 is same.
but s1==s2 returns true and
s1==s3 returns false
does hashcode value play any role in the object identity check?
Gajendra Kangokar wrote:does hashcode value play any role in the object identity check?
No. It shouldn't even play a role in an equals() check, although it should be consistent with that.
As you've seen, s3 forces the creation of a new String, whereas s2 allows the reuse of an existing String as an optimisation. The other important difference is that there's never any reason to use the s3 style.