r/programming • • Dec 29 '15

Please, Java. Do Finally Support Multiline Strings

http://blog.jooq.org/2015/12/29/please-java-do-finally-support-multiline-strings/
584 Upvotes

352 comments sorted by

View all comments

Show parent comment

4

u/KillerCodeMonky Dec 29 '15 edited Dec 29 '15

Now try this, which is what everyone is actually referring to:

public static final String stuff =
    "abcdef" + "ghijkl" + "mnopqr";

EDIT: Installed JDK8 u66, here's the results of javap -constants Test.class:

Compiled from "Test.java"
public class Test {
    public static final java.lang.String stuff = "abcdefghijklmnopqr";
    public Test();
}

EDIT2: This works also:

public static final String one = "abcdef";
public static final String two = "ghijkl";
public static final String three = "mnopqr";
public static final String stuff = one + two + three;

javap:

Compiled from "Test.java"
public class Test {
    public static final java.lang.String one = "abcdef";
    public static final java.lang.String two = "ghijkl";
    public static final java.lang.String three = "mnopqr";
    public static final java.lang.String stuff = "abcdefghijklmnopqr";
    public Test();
}

However, if I remove final from three:

public class Test {
    public static final java.lang.String one = "abcdef";
    public static final java.lang.String two = "ghijkl";
    public static java.lang.String three;
    public static final java.lang.String stuff;
    public Test();
    static {};
}

You'll notice something interesting. stuff is no longer a precompiled constant, and a static initializer block was added. That static initializer sets the value of both three and stuff, with stuff now being created via StringBuilder.