3

What arguments can You give for the use one or another variant that is better, faster, more correct.

First variant:

StringBuffer sql = new StringBuffer("SELECT DISTINCT f.ID ")
    .append("FROM FIRST_TABLE F ")
        .append("LEFT JOIN SECOND_TABLE s ON f.ID = s.F_ID ")
    .append("WHERE ")
        .append("F.BOOL = 1 ")
        .append("AND S.DATE IS NOT NULL ")
        .append("AND S.CLOSED = 0 ");

Second variant:

String sql = "SELECT DISTINCT f.ID " +
             "FROM FIRST_TABLE F " +
                "LEFT JOIN SECOND_TABLE s ON f.ID = s.F_ID " +
             "WHERE "
                "F.BOOL = 1 " +
                "AND S.DATE IS NOT NULL " +
                "AND S.CLOSED = 0";

*for note: Class String and Class StringBuffer.

1

2 Answers 2

11

The second is better:

  • It's clearer (more of the code has to do with what you want)
  • It's more efficient, as all the concatenation is being done at compile-time

Even if execution-time concatenation was required (e.g. you had a variable in there), the compiled code would use a StringBuilder or StringBuffer where it needed to (depending on the version of Java you're targeting).

Mind you, if you're executing a database query, the efficiency is going to be utterly insignificant.

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

14 Comments

Ok thx, but what about performance, most interested this part of the question?
@GanGnaMStYleOverFlowErroR: No, it wouldn't. In fact, in this case it would generate fewer strings, because (for the third time) the concatenation would be done at compile-time. And again, even if it weren't, the compiler would do the right thing. Please read yoda.arachsys.com/java/strings.html and if you have a specific situation in mind, compile it and then use javap to see what would actually happen.
@enjoyLife: No, you'll still get the string constants with StringBuffer or StringBuilder. Please read the link in an earlier comment... if you're providing everything in a single expression, then using concatenation will be at least as efficient. StringBuilder is better for things like loops, where you can't express the whole operation in a single expression.
+1 Just because using StringBuilder direct is more complicated won't make it more effficient. Using + will either a) be simplified at compile time, or b) do exactly the same thing with less code. In any case, no matter how large the string generated is, it is unlikely to matter compared with the cost of passing, parsing, and performing the SQL.
@enjoyLife: Using concatenation, the compiler gets to to what it can at compile-time, and do the rest using a StringBuilder at execution time. So it's at least as efficient, and more readable.
|
0

You should not be wasting your time worrying about how to concatenate a few strings. Use 2 and keep it clear. Using StringBuilder/buffer is not the mark of a great programmer, if that's what you thought.

Worry about the vaguely defined requirements and the things behind schedule.

Try this ->

long finalTime1 = 0; { long initialTimeTest = System.currentTimeMillis(); for( int index = 0; index < 10000; index++ ){ StringBuilder sb = new StringBuilder("Hello, ").append("World"); System.out.println(sb.toString()); } finalTime1 = System.currentTimeMillis() - initialTimeTest;

}

long finalTime2 = 0; { long initialTimeTest = System.currentTimeMillis(); for( int index = 0; index < 10000; index++ ){ String sb = "Hello, " + "World"; System.out.println( sb ); } finalTime2 = System.currentTimeMillis() - initialTimeTest; }

System.out.println( finalTime1 ); System.out.println( finalTime2 );

Results:

... Hello, World Hello, World 245 148

Did you think string buffer was faster ??

You are breaking the mother of all rules: Keep it Simple. -

For mundane string handling there is no reason why to use StringBuilder. It just adds unnecessary complexity to a mundane task.

We need to think BIG, think in the overall business impact of the module to the project. Discussing whether we shall assemble a few strings with StringBuilder/Builder or String is thinking little, - don't do that.

To your question about performance, I recommend:http://cfd.gmu.edu/~jcebral/academics/csi702/notes/02-optimization.pdf

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.