1

Imagine that you want to display a welcome message to a game but are required to keep the lines in the source code at a maximum length of 80 characters width. In the game the length of the lines are of no importance. An example of the welcome message would be:

private void welcomeMessage() {
    System.out.println("Welcome to the game. This message should learn me how to create a long messages like this in a read-able fashion. Imagine there is a limit of 80 characters per line for a programmer to see. How to fix this efficiently? This is one large line in my IDEA. ");
}

This line is way too long for it to be 80 characters per line of code, as you can see by the scroll bar. I previously learned to simply do something like this:

private void welcomeMessage() {
    System.out.println("Welcome to the game. This message should learn me" +
            " how to create a long messages like this in a read-able " +
            "fashion. Imagine there is a limit of 80 characters per line for" +
            " a programmer to see. How to fix this efficiently? This is one " +
            "large line in my IDEA. ");
}

However I find this solution ugly, this creates a dependency which is so annoying. If I add to "Welcome to the game. This message should learn me" just one word I have to change all the other lines, so all lines after a line depends on it.

I can also imagine there are penalties just for making it readable in this way. It makes me wonder:

  1. Does the compiler append all lines directly since they themselves do not depend on other variables or does it happen at runtime? (I am afraid that it would call the concatenation operator function ('+') 4 times)
  2. Would a StringBuffer with .append() be more efficient in this example because it does not create 5 strings but instead appends it to the StringBuilder object?

Imagine this was a real game, would you then just make a txt text file and import it with a StringBuffer etc. to display it on the screen? Or how else would you display longer text messages?

Joop
  • 3,706
  • 34
  • 55
  • 1
    The String concatenation is actually turned into `StringBuilder.append()` behind the scenes. I would personally use a StringBuilder and append for each line so it looks more clean (remembering to put a space at the end of beginning of each one). Or, you can use Apache Commons Configurations and read in the value from a `.properties` file. – Ascalonian Dec 22 '14 at 13:20
  • At what point does this "requirement" come into play? If only at the very end of an assignment, write your code and only adjust formatting when done. If not, how often *do* you change strings -- often enough to make it a practical problem? – Jongware Dec 22 '14 at 13:24
  • @Ascalonian Thank you. It took a while to find that answer form that page. Here is the answer to 1. Strings computed by constant expressions (§15.28) are computed at compile time and then treated as if they were literals. Strings computed by concatenation at run time are newly created and therefore distinct. You can see here what a String literal is: http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.28 – Joop Dec 22 '14 at 13:25
  • @Jongware In my experience it's a real practical problem, I changed the help function for example of my program many times because every time I added new features I had to change that also. But because I also noticed that this must be how unprofessional people do this I included the last question, namely how you would do it in a real game.. – Joop Dec 22 '14 at 13:27
  • 1
    In that case you might want to consider using an external file. (Note that something like multilingual versions suddenly becomes possible, and almost for free as well.) – Jongware Dec 22 '14 at 13:29
  • 1
    @Ascalonian, only if you involve variables. Compile time constants are evaluated by the compiler. See my answer. – aioobe Dec 22 '14 at 13:29
  • The '+' concatenation operator is indeed translated to a StringBuilder by the compiler unless it is in a loop, http://stackoverflow.com/questions/1532461/stringbuilder-vs-string-concatenation-in-tostring-in-java – Joop Dec 22 '14 at 14:04

1 Answers1

2

Does the compiler append all lines directly since they themselves do not depend on other variables or does it happen at runtime? (I am afraid that it would call the concatenation operator function ('+') 4 times)

The compiler will concatenate the strings. No runtime overhead.

Would a StringBuffer with .append() be more efficient in this example because it does not create 5 strings but instead appends it to the StringBuilder object?

No.


Demo:

class Test {
    public static String test = "a" + "b" + "c";
}

The command

javac Test.java && javap -c -v Test

yields the following output

[...]
Constant pool:
   #1 = Methodref          #5.#15         //  java/lang/Object."<init>":()V
   #2 = String             #16            //  abc
   #3 = Fieldref           #4.#17         //  Test.test:Ljava/lang/String;
[...]

Code:
  stack=1, locals=0, args_size=0
     0: ldc           #2                  // String abc
     2: putstatic     #3                  // Field test:Ljava/lang/String;
     5: return     

[...]
aioobe
  • 413,195
  • 112
  • 811
  • 826