Home > Java > A footnote about String equality testing

A footnote about String equality testing

It occurs to me that when I was writing about the string equality testing functions in an earlier post that aside from efficiency there are an overwhelming number of good reasons for preferring the String.equals() to == as a test of equality in a generalised function in a class containing String manipulation routines.

The crux of the matter is this: .equals() will always tell you the truth about the equality of the contents of Strings being compared since it does a byte by byte comparison of both Strings under consideration, hence it is inherently less efficient than == which does a comparison of the object references and not the equality of the contents. == will only return true if the Strings being compared are the same object.

Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: