In the above code s1==b1 prints "equal String" while s==b does not print "equal"
could anyone explain to me the reason for this . Thanks in advance
Joined: Jan 15, 2006
== tests if the objects being compared are actually the same object. In your example, s and b are different objects with the same contents, so while they pass the equals() test because they have the same contents, they produce false when using ==.
s1 and b1 both refer to the same object in the string literal pool, so they pass both == and equals().
"new" will create to new objects, that is why "==" returns false.
However, using "=" will get an existing object from the pool, or create a new one if it does not exist. There is already a String whose value is "abc", so it will return the same reference to that String. Same reference, means that "==" will return true.