r/learnjava • • Jan 12 '21

Java Interview Questions and Answers

Came across this while preparing myself, you can refer to this if you are preparing for Java interview. Hope you find this useful. :)

68 Upvotes

17 comments sorted by

View all comments

Show parent comments

1

u/Kombatnt Jan 13 '21

I would argue that if you find yourself composing a String in a loop, that constitutes a code smell, and I would seriously consider other/better ways of achieving the desired result. Perhaps a stream operation, a .forEach(), or something else.

Finally, if none of that was appropriate, and a loop really was the best way to do it, I would still use String concatenation unless we're talking about a seriously large number of loop iterations (thousands or more). Readability trumps performance, until performance becomes noticeable.

2

u/nutrecht Jan 13 '21

I would argue that if you find yourself composing a String in a loop, that constitutes a code smell

You can argue all the nonsense you want.

1

u/Kombatnt Jan 13 '21 edited Jan 13 '21

OK, well you tell me which is easier to read and understand:

StringBuffer customerNames = new StringBuffer();
for (Customer customer : customerService.getActiveCustomers()) {
  customerNames.append(customer.getName());
  customerNames.append(", ");
}
String result = customerNames.toString();
// Trim off trailing ", ". Hope there's at least 1!
result = result.substring(0, result.length() - 2);
return result;

vs.

return customerService.getActiveCustomers()
                      .stream()
                      .map(Customer::getName)
                      .collect(Collectors.joining(", "));

3

u/nutrecht Jan 13 '21

You were talking about string concatenation in the original post, not using streams. I'm perfectly fine with the stream version (that's what I'd use), but it's simply wrong to come on a beginner forum and tell beginners they should not use StringBuilder.

In addition; when performance is critical it's often better still to use StringBuilder over streams.

StringBuffer is almost never used.

1

u/Kombatnt Jan 13 '21

We'll agree to disagree. I think it's wrong to tell a beginner not to use the super-obvious and easy-to-understand string concatenation, instead directing them to an entirely new class they need to learn, in service of "performance." In my experience, performance is almost never an issue. "Make it work, make it right, then make it fast" is advice given in order of priority. In the words of Knuth, "Premature optimization is the root of all evil." Even back when performance actually mattered, he understood that it is of lower priority that readable, correct code. Now, when you could concatenate 1,000 Strings using the "inefficient" string concatenation, and have it complete too quickly to even measure, I see no reason to complicate the code in a misguided pursuit of absolute optimal speed.

2

u/nutrecht Jan 13 '21

We'll agree to disagree

No, you're just wrong. And seriously I'm getting pretty fed up with you.

It's very important for people who are learning (and this is learnjava after all) to understand when you should and when you should not use StringBuilders. Your initial post gave information that was just plain wrong. If you can't handle a small correction from someone who's been a Java dev for 20 years, you have no place posting here.

1

u/Kombatnt Jan 13 '21

I apologize, I wasn't trying to provoke anyone. You're right, I neglected to consider the situation where a String is being built up in a loop. I'm not denying that a StringBuilder would be faster than String concatenation in that situation - I'm simply saying that (at least in my opinion), it doesn't matter. String concatenation is more readable, so that's the approach I would prefer.

In my experience, and in virtually all literature I've read, performance concerns take a distant back seat to readability and maintainability. I merely meant to push back a little against the dogmatic pursuit of squeezing every last nanosecond of performance out of an otherwise simple construct.

→ More replies (0)