I was creating a logger in my java-app (with NetBeans as IDE) when suddenly I saw a warning saying: “Inefficient use of string concatenation in logger”.
My oringinal code is
srcLogger.getLogger().log(Level.INFO,"UploadBean.doUpload completado [" + file.getName() + "]\n");
but NetBeans suggested to convert it to a template (what a “template” means here?) giving this code:
srcLogger.getLogger().log(Level.INFO, "UploadBean.doUpload completado [{0}]\n", file.getName());
What’s the different between these two ways of concatenation, I never used the latter though.
Cheers.
I’d ignore the warning (and switch if it off, if possible). The concatenation is not that inefficient, because modern compilers replace it with an efficient implementation based on
StringBuilder(you’ll see it if you look at the classfile’s bytecode).The suggested replacement doesn’t concatenate Strings but it requires some extra processing to parse the template and merge it with the parameters.
Netbeans, that’s a bad advice.
This is true for Java 1.5+. Older versions of Java (may) create a lot of unused
Stringinstances during concatenation..