0

I saw the java code in my project which constructs a file name from various parameters.The java code is like this

String file = null;
String fileDirSeperator = System.getProperty("file.separator");
String pwd = System.getProperty("user.dir");
file = pwd + fileDirSeperator + "properties" + fileDirSeperator + folder_name + file_name;

I want to know if I should you StringBuffer instead of String. I know that appending anything to String will create new object so I think I should you StringBuffer.

May I know your suggestion?

Also,

 file = pwd + fileDirSeperator + "properties" + fileDirSeperator + folder_name + file_name;

in the above file name construction, will 6 objects be created(one for each variable)?

Anand
  • 20,708
  • 48
  • 131
  • 198
  • 4
    If you're concatenating only 6 Strings, I wouldn't care that much about performance. Use `+` and don't over-complicate it. – Maroun May 21 '14 at 08:09
  • 2
    @BobbyDigital not for this case. The generated bytecode will already use `StringBuilder` for you. – Luiggi Mendoza May 21 '14 at 08:10
  • @LuiggiMendoza: I saw that! – ChiefTwoPencils May 21 '14 at 08:10
  • Really the only time you'll have to be concerned about string concatenation with `+` is in loops/recursive calls/etc., especially if you save the result into a temp variable. For one-off things like this, you'll end up writing the same code as what the compiler will generate anyways. – awksp May 21 '14 at 08:13

3 Answers3

6

This is irrelevant. Unless you're doing this in a loop you run thousands of times, the performance doesn't matter. The String concatenation syntax would be the most straight forward then.

When performance is an issue with concatenation, use StringBuilder.

Kayaman
  • 72,141
  • 5
  • 83
  • 121
0

Use StringBuilder instead of the options you mentioned.

Of course with such a small use case, it doesn't really matter

geoand
  • 60,071
  • 24
  • 172
  • 190
  • 2
    -1: In this case, the compiler will use `StringBuilder` for you. Check the generated bytecode. Use `StringBuilder` only when concatenating through a loop, when the compiler cannot do any code optimization. – Luiggi Mendoza May 21 '14 at 08:09
  • @LuiggiMendoza Could you point to a reference? – geoand May 21 '14 at 08:10
  • 1
    @LuiggiMendoza Seriously, no kidding that it's not hard to replicate!!! I was asking if you have any authorative links to point to that explain that choice. Thanks for the -1 by the way :) – geoand May 21 '14 at 08:12
  • It takes less than 5 minutes to replicate. Anyway, here's a link: http://stackoverflow.com/q/47605/1065197 – Luiggi Mendoza May 21 '14 at 08:13
  • @LuiggiMendoza I would say that it takes less than 2 to replicate, but as I explained, that was not my question. Thanks for the link :). And by the way, not every one is in the habbit of reading bytecode or decompiling class files for such simple use cases :) – geoand May 21 '14 at 08:15
  • @LuiggiMendoza Good for you! Many people would rather indulge in other development related things and only get down to the bytecode-decompiling level when that's absolutely needed. Personally I would much rather try out what is new in Spring 4.0.5 than go through unsexy bytecode – geoand May 21 '14 at 08:18
  • @LuiggiMendoza Spring was just an example! There is so much other stuff out there as I am sure you know, that in my opinion is so much more interesting that bytecode – geoand May 21 '14 at 08:21
  • @LuiggiMendoza I can't agree with you more, that some subtle problems can perhaps only be spotted at the bytecode level. I am all for bytecode reading, but what I am saying is that I personally will not indulge in it's pleasures unless I really need to or unless I am done checking out all the other cool stuff around (which will never happen :)) – geoand May 21 '14 at 08:27
0

Having benchmarked this across tens of thousands of operations, you may as well use string concatenation. From Java 6 the performance has greatly improved.

http://www.javacodegeeks.com/2013/03/java-stringbuilder-myth-debunked.html

http://www.code-thrill.com/2012/08/stringbuilder-optimizations-demystified.html

Leon
  • 12,013
  • 5
  • 36
  • 59
  • Both your links state that it's better to use StringBuilder. The links are good, but your answer contradicts them. What gives? – Kayaman May 21 '14 at 08:17
  • I should have pointed out that even though the article say use StringBuilder, the first one says do it because of JDK5 and the second one points to the reason to use StringBuilder when constructing strings using if statements or for loops. The way the poster uses strings he should stick with concatenation – Leon May 21 '14 at 08:21