String comparison and String interning in Java -
when should 1 compare string
s objects , when should 1 use equals
method? make sure, use equals
, doesn't seem efficient. in situations can string1 == string2
safe use?
thanks!
you should always use equals
. can string1 == string2
work if:
- you've made sure you've got distinct values in other way (e.g. you're using string values fetched set, comparing them other reason)
- you know you're dealing compile-time string constants
- you've manually interned strings yourself
it doesn't happen often, in experience.
Comments
Post a Comment