Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>Edit: Even the compiler doing this change (that I didn't know), using String concatenations is SLOWER then using StringBuilder.</strong> (Wrong => <em>No, the compiler does not use StringBuilder/StringBuffer</em>). You can test it just creating a simple benchmark. The + operator ir "overloaded" in Java for Strings, but the language does not support operator overload.</p> <p>Try something like this to test the StringBuilder/StringBuffer thing:</p> <pre><code>String str = ""; StringBuffer sbf = new StringBuffer(); StringBuilder sb = new StringBuilder(); int nTests = 100; int nConcats = 1000; long initialTime = 0L; long afterStrTime = 0L; long afterSbfTime = 0L; long afterSbTime = 0L; for ( int i = 0; i &lt; nTests; i++ ) { initialTime = System.currentTimeMillis(); str = ""; sbf = new StringBuffer(); sb = new StringBuilder(); for ( int j = 0; j &lt; nConcats; j++ ) { str += "foo"; // or str = str + "foo" } afterStrTime = System.currentTimeMillis(); for ( int j = 0; j &lt; nConcats; j++ ) { sbf.append( "foo" ); } afterSbfTime = System.currentTimeMillis(); for ( int j = 0; j &lt; nConcats; j++ ) { sb.append( "foo" ); } afterSbTime = System.currentTimeMillis(); } System.out.printf( "%d milliseconds to perform %d concatenations on String\n", afterStrTime - initialTime, nConcats ); System.out.printf( "%d milliseconds to perform %d concatenations on StringBuilder\n", afterSbfTime - afterStrTime, nConcats ); System.out.printf( "%d milliseconds to perform %d concatenations on StringBuffer\n", afterSbTime - afterSbfTime, nConcats ); </code></pre> <p>You will see that StringBuilder can be faster than StringBuffer, since it is not synchronized (StringBuffer is), and both are faster than String.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload