The efficiency is not much different due to optimizations in modern Java compilers, but it does not gain much flexibility. The only thing it lets you do is turn off printing in one location rather than many.
You would be better served by delegating to a logging framework. Java has built-in logging, and there are other frameworks such as Log4j.
Using a logging framework you gain several abilities your current code does not:
You can log to arbitrary streams: standard out, standard error, a file, a socket, etc.
You have additional diagnostic levels. Rather than on or off you can have error, warning, info, et al and you can control which diagnostic levels are output.
Logging messages can include additional information such as the class and line number where the logging occurred (if you pass along a Throwable
), and the date and time.
All of the configuration is done through, well, configuration, not code. Rather than commenting out a line of code and rebuilding, you can edit an XML or properties file and you are done.
Once you get in the hang of using a logging framework it is just as easy as using System.out.println()
but there is far more capability if you need it.
Finally, a logging framework typically requires a tiny amount of initialization but adds little or no noticeable overhead to the running process when you invoke its functionality.