Readers will note the recursive nature of the last example, as the constructor's argument ("Foo!") is itself a string literal. In most implementations of String, calling new String(String s) will copy the parameter String's implementing character array using native array copy methods, making sure that the array isn't longer than the stored String.

This will work, but exceptionally lazytypists (like myself) object on moral (read: keystroke count) grounds. One of two approaches will make the world Safe for DemocracyTM:

Compare your String to a static variable you've stashed somewhere. Either a public static final in an abstract class or a simple String in an interface will work. I'm partial to the interface approach, myself. Your comparision might look like this:

if (IConstants.FooConst.equals(foo)) { ... }

Or you can be weird. String literals in Java ("Foo!" is a String literal) are perfectly valid instances of java.lang.String, so: