As is well known
LOGGER.debug("The parameter p1 cannot be " + p1 +
", it must be between " + P1_MIN + " and " + P1_MAX);
Has the disadvantage that string concatenation is done even if the result will be discarded due to logger settings. To avoid this, the following boilerplate is popular
if (LOGGER.isDebugEnabled())
{
LOGGER.debug("The parameter p1 cannot be " + p1 +
", it must be between " + P1_MIN + " and " + P1_MAX);
}
Which avoids it at the cost of three extra lines.
slf4j (and others) offer an alternative using varargs (and overloading):
LOGGER.debug("The parameter p1 cannot be {} , it must be between {} and {}",
p1, P1_MIN, P1_MAX);
This looks much cleaner but there is an Object array getting created to pass variable number of parameters.
How expensive is this object array creation? Does its cost outweigh the advantage of reducing lines of code?
I suspect the varargs/formatter based methods are created exactly to address the above boilerplate so even though slightly subjective replies from experts will help great many numbers of programmers. Also, IMHO it is not subjective if object array creation is either very cheap (which is my hope) or very expensive.