[SonarQube ]: Format specifiers should be used instead of string concatenation.

Format specifiers should be used instead of string concatenation.

Solution

String concatenation means LOGGER.info("The program started at " + new Date());

Built in formatting of logger means
LOGGER.info("The program started at {}", new Date());

very good article to understand the difference http://dba-presents.com/index.php/jvm/java/120-use-the-built-in-formatting-to-construct-this-argument

Complete Understanding

tl;dr

  • so if you disable the logger.info() and there is some concetation or method is there inside logger.info that will still get executed so that why we say do not use string concetenation in the logging since this can take up huge space since each concatenation will take up some space and that will take the space from the string pool that is part of heap and heap is limited

  • if logging info level is enabled that in that case all the things inside the get executed first and then logger.info get executed

Two cases

  1. Logging info level is enabled LOGGER.info("The program started at {}", new Date()); --> log will be printed

  2. Logging info level is disabled `LOGGER.info("The program started at {}", new Date()); --> log will not be printed and also there will be no concatenaiton in the background ` so it is a win win situtation. :)