Why does the following expression evaluate as false
aStr.replace("H", "H") == "Hello"
while this alternate expression evaluates as true?
aStr.replace('H', 'H') == "Hello"
Java caches String
literal in String pool, so if there is no change in String
it would take an immutable instance from pool and so that both refers to same instance in memory and thus ==
on reference returns true
So to compare String Object you should use equals()
method / compareTo()
method
Also See
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With