4

I'm by no means a java programmer, so this may seem pretty basic.

Which of these are 'better' when you want to keep your code lines short.

String str = "First part of a string.";
str += " Second part of string.";

or

String str = "First part of string." +
" Second part of string."

I guess my question is do both the += and + make a new String object? If they do then neither really are better, it would just be a matter of preference. The example I gave would be a good example of real world use. I don't want a comparison of doing a concatenation 3 times to 1000 times with either method.

Thanks

3
  • 2
    I can't speak for the actual implementation, but it seems to me like the second one would make it pretty easy to optimize the concatenation away, and just consider both parts of the string a single literal. Commented Apr 13, 2012 at 16:16
  • Awesome guys, thanks! I'm a ruby developer and using << to concat two strings doesn't create a new object, just modifies the one you added to. So I was wondering how Java handled the similar problem. Commented Apr 13, 2012 at 16:23
  • 2
    For general string building (when you need to concatenate lots of strings), StringBuilder is a great tool to help avoid the expenses of raw concatenation. Commented Apr 13, 2012 at 16:24

5 Answers 5

3

I prefer the 2nd method. The reason is that the compiler will likely combine the result of the concatenation into a single string at compile time while the 1st method may be done at run-time (depending on the actual implemention.) It's a small thing unless you're doing something millions of times, however.

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

1 Comment

Will definitely combine the result of the concatenation at compile time. This is required behaviour.
2

The Java compiler is actually required to concatenate the second example at compile time. See 15.28. Constant Expressions and 3.10.5. String Literals.

1 Comment

The compiler is required to concatenate it at compile time. Nothing to do with the JVM yet.
0

Here's what I get when I compile then decompile this:

public static void main(String[] args) {
    String str = "First";
    str += " Second";
    System.out.println(str);

    String str2 = "First" + " Second";
    System.out.println(str2);
}

Becomes:

public static void main(String args[]) {
    String s = "First";
    s = (new StringBuilder()).append(s).append(" Second").toString();
    System.out.println(s);
    String s1 = "First Second";
    System.out.println(s1);
}

So the second method is better.

Comments

0
StringBuilder sb = new StringBuilder();
sb.append("First part");
sb.append("Second part");
System.out.print(sb.toString());

Comments

0

Following Java's coding conventions:

String str = "First part of string. "
             + "Second part of string.";

Make sure the '+' operator begins the next line this improves readability. Using this style allows for readable and efficient code. https://www.oracle.com/technetwork/java/javase/documentation/codeconventions-136091.html#248

Hope this helps!

Happy coding,

Brady

Comments

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.