7

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.

1
  • by the way, similar will happen for int i = 40 + 2; - the compiler will compile it as int i = 42; Commented Dec 26, 2024 at 8:33

3 Answers 3

11

Yes, this will be handled by the constant expression part of the Java Language Specification. In particular see part 15.18.1. String Concatenation Operator +

Sign up to request clarification or add additional context in comments.

Comments

9

Strings computed by constant expressions (§15.28) are computed at compile time and then treated as if they were literals.

Show that quote from the JLS to anyone who 'challenges' you.

Comments

4

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"

1 Comment

you can also use javap to check the compiled code

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.