This week's book giveaway is in the OCAJP forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide 1Z0-808 and have Jeanne Boyarsky & Scott Selikoff on-line! See this thread for details.
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.