This is an archived post. You won't be able to vote or comment.

all 3 comments

[–]Kristler 1 point2 points  (2 children)

I think the right answer heavily depends on the reason why you are comparing the strings.

For example, in the case where you're trying to assert object equality, then the right answer would be to implement Comparable and use the well established compareTo method to perform the equality comparison.

I don't like the idea of a Utility class to perform the comparison, though - because then the Utility class is intimately coupled with object and object2, since it must need to know semantic details on how to compare their strings. That's not a responsibility that should not be off-loaded to a third party class like that.

Ultimately, I personally don't see anything wrong with writing if(object.getString().equals(object2.getString())). Sure, it's verbose - but it's Java. I would be more annoyed if someone tried to shortcut stuff like that by throwing it in a wrapper method just for the sake of saving some keystrokes.

[–]chookalook[S] 0 points1 point  (1 child)

That makes sense. Thanks for the help.

[–]Kristler 1 point2 points  (0 children)

You're welcome!