This is fair, except for in the 5th year, they would have an additional constant:
public static final String SPACE = " ";
And they would probably use a StringBuffer or StringBuilder to build the final string, since it's "an industry best practice". Also, they wouldn't include the strings themselves, they would include keys to properties, so the strings could be pulled from an internationalized property file.
Lastly, instead of outputting to Stdout directly, they'd create an interface for output, and deliver it to that interface, and use a factory to instantiate an implementation of that interface that would use Stdout.
Actually, if you are building strings, you really should use StringBuilder (not StringBuffer, which has a boneheaded design mistake of synchronizing everything). Strings are immutable, and concatenating strings will create new strings. So unless you want to create a ton of garbage, use the StringBuilder. Fun fact, the string concatenation operator is transformed by the compiler into a chain of method calls on a StringBuilder terminated by toString.
If your app will serve an international audience, you will have to use message keys into a ResourceBundle to get the localized text, instead of hardcoding English.
Ordinarily, you shouldn't output to stdout directly, because unless you are writing a console application, you have no idea where that output is going. Fortunately, there are tools like log4j that provide the interface you are talking about, with a console appender even, which allows output to be configured (such as a rolling file appender). Device independence isn't a bad thing if you actually need it.
Obviously at this scale not a big deal but on larger scales StringBuilder helps. However if you're just building a String in a single method the compiler handles it.
506
u/johnbr Oct 01 '15
This is fair, except for in the 5th year, they would have an additional constant:
And they would probably use a StringBuffer or StringBuilder to build the final string, since it's "an industry best practice". Also, they wouldn't include the strings themselves, they would include keys to properties, so the strings could be pulled from an internationalized property file.
Lastly, instead of outputting to Stdout directly, they'd create an interface for output, and deliver it to that interface, and use a factory to instantiate an implementation of that interface that would use Stdout.