String comparison and String interning in Java
When should one compare String s as objects and when should one use their equals method? To make sure, I always use equals , but that doesn't seem very efficient. In what situations can I be certain that string1 == string2 is a safe to use? Thanks! You should almost always use equals . You can be certain that string1 == string2 will work if: You've already made sure you've got distinct values in some other way (e.g. you're using string values fetched from a set, but comparing them for some other reason) You know you're dealing with compile-time string constants You've manually interned the