Equality Testing with Hardcoded Strings

If you are going to use a String equality comparison that involves a hardcoded string, you might as well save yourself from some NullPointerExceptions and the extra null pointer check, put the hardcoded string as the method call-e instead of the parameter.

Instead of:

if ( foo.equals( "some static string" ) ){...}

or...

if ( foo != null && foo.equals( "some static string" ) ){...}

use:

if ( "some static string".equals( foo ) ){...}

One thought on “Equality Testing with Hardcoded Strings

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>