1
String today = someSimpleDateFormat.format(new Date());
Calendar rightNow = Calendar.getInstance();
int hour = rightNow.get(Calendar.HOUR_OF_DAY);
int minute = rightNow.get(Calendar.MINUTE);
String hourString = String.valueOf(hour);
String minuteString = String.valueOf(minute);

if(hourString.length() == 1){
    hourString = '0'.concat(hourString);
}

if(minuteString.length() == 1){
    minuteString = '0'.concat(minuteString);
}

String dayHourMinute = today.concat("_").concat(hourString).concat("_").concat(minuteString);       

I could have used '+' operator. Would there be any performance issue if I have lots of string concatenation in the program and I use '+' operator over the 'concat' method or viceversa?

4 Answers4

14

Either way you'll be creating a lot of unnecessary temporary Strings. Strongly recommend using StringBuilder instead. The compiler will actually use temporary StringBuilder instances when you use the + operator, but it doesn't have the broader vision of what you're trying to achieve and is limited in terms of how much it can optimize the StringBuilder use, so you'll almost always do a better job making it explicit.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • 1
    If you are focusing on performance (it is a commonly used or key area of code) you would also preallocate the size of the buffer (initial capacity in the constructor) used for the string so the buffer doesn't have to be resized. – Jim Rush Dec 07 '09 at 12:07
  • Shlemiel The Painter strikes again! (http://www.joelonsoftware.com/articles/fog0000000319.html) – Rob Grant Dec 07 '09 at 12:17
  • @Jim Rush: Yes indeed, that's part of having a greater context to work with. – T.J. Crowder Dec 07 '09 at 12:34
4

I think both are more or less equivalent. However, if your concerned about performance, you should use StringBuilder for string concatenation.

sfussenegger
  • 35,575
  • 15
  • 95
  • 119
4

It doesn't really matter:

Yes, you should avoid the obvious beginner mistakes of string concatenation, the stuff every programmer learns their first year on the job. But after that, you should be more worried about the maintainability and readability of your code than its performance. And that is perhaps the most tragic thing about letting yourself get sucked into micro-optimization theater -- it distracts you from your real goal: writing better code.

Jerome
  • 8,427
  • 2
  • 32
  • 41
  • Cool article. I make decision by folow rule: 1. more readable - more good. 2. if more than 8 contactions - always use StringBuilder. More pretty for me. And this is a sing "Hey! I'm *building* string here". – St.Shadow Dec 07 '09 at 12:17
  • My favourite quote from this article: "Memory allocations are [..] far from free." ... hilarious :D – sfussenegger Dec 07 '09 at 12:21
  • Yes, No sign of Java at the blog entry (just at the comments). No Java code, looks like C# or similar. Even if the code would be Java code and he tested it with Java, the benchmarks would be nonsense, as no warmup has been made. The article is just not suitable for Java. – Hardcoded Dec 07 '09 at 12:59
  • The point of the article is that it is useless to do a benchmark on this, because it is a micro-optimization, and micro-optimization are a Bad Thing. The language used in the code doesn't matter. – Jerome Dec 07 '09 at 13:15
  • What defines a micro optimizations? I already spead up parsers by factors of 2-3 only by using StringBuffer (no StringBuilder at that time). I don't think that it is a micro-optimization if String concatination is a main task. Additional, the syntax isn't that different between +, String.concat and StringBuffer/Builder.append. – Hardcoded Dec 07 '09 at 13:25
  • That's true, in the good old days, it was considerable optimization to use StringBuffer - but modern compilers replace `+` with StringBuilder#append chains anyway so we can return to improving readability (in most cases). But I think, the answers here are reference: http://stackoverflow.com/questions/1825781/when-to-use-stringbuilder – Andreas Dolk Dec 07 '09 at 14:10
4

If you don't have performance issues, consider the following alternative, which I find easier to read:

String dayHourMinute = 
     String.format("%s_%s_%s", today, hourString, minuteString);
String evenBetter = 
     String.format("%s_%02d_%02d", today, hourString, minuteString);
// thanks to hardcoded!
Andreas Dolk
  • 113,398
  • 19
  • 180
  • 268
  • 3
    Even better: String.format("%s_%02d_%02d", today, hour, minute) – Hardcoded Dec 07 '09 at 12:47
  • 1
    Even possible: String.format("%1$tY-%1$tm-%1$td, %1$tH:%1$tM", rightNow) read more on http://java.sun.com/j2se/1.5.0/docs/api/java/util/Formattable.html – Kennet Dec 07 '09 at 14:28