Search code examples
javastring

Java Compiler String optimization


It seems reasonable to me that the compiler is going to take something like this:

log.info("A really long logger message that is kind of a pain in the tucous " + 
    "and violates formatting standards by making the line to long");

and compile the two Strings into one. I'm pretty sure this is true but I would like to have my ducks in a row if anyone brings it up.


Solution

  • To check if what JLS says about constant expressions is true I complied this code, Test.java

    public static void main(String[] args) {
        log.warning("123" + "456");
    }
    

    then decompile Test.class with Jad and got this

    public static void main(String args[])
    {
        log.warning("123456");
    }
    

    that is, in Test.class there is only one literal "123456"