views:

221

answers:

4
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?

+13  A: 

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
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
Shlemiel The Painter strikes again! (http://www.joelonsoftware.com/articles/fog0000000319.html)
Robert Grant
@Jim Rush: Yes indeed, that's part of having a greater context to work with.
T.J. Crowder
+4  A: 

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

sfussenegger
+3  A: 

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
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
My favourite quote from this article: "Memory allocations are [..] far from free." ... hilarious :D
sfussenegger
NoJavaDetectedError ;-)
Hardcoded
So...? Did you read/understand the article?
Jerome
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
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
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
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_D
+4  A: 

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_D
Even better:String.format("%s_%02d_%02d", today, hour, minute)
Hardcoded
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