Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>To see what the Java compiler produces it is always useful to use <code>javap -c</code> to show the actual bytecode produced:</p> <p>For example the following Java code:</p> <pre><code>String s1 = "Four" + 4; int i = 4; String s2 = "Four" + i; </code></pre> <p>would produce the following bytecode:</p> <pre><code> 0: ldc #2; //String Four4 2: astore_1 3: iconst_4 4: istore_2 5: new #3; //class java/lang/StringBuilder 8: dup 9: invokespecial #4; //Method java/lang/StringBuilder."&lt;init&gt;":()V 12: ldc #5; //String Four 14: invokevirtual #6; //Method java/lang/StringBuilder.append:(Ljava/lang/ String;)Ljava/lang/StringBuilder; 17: iload_2 18: invokevirtual #7; //Method java/lang/StringBuilder.append:(I)Ljava/lan g/StringBuilder; 21: invokevirtual #8; //Method java/lang/StringBuilder.toString:()Ljava/la ng/String; 24: astore_3 25: return </code></pre> <p>From this we can see:</p> <ul> <li>In the case of <code>"Four" + 4</code>, the Java compiler (I was using JDK 6) was clever enough to deduce that this is a constant, so there is no computational effort at runtime, as the string is concatenated at compile time</li> <li>In the case of <code>"Four" + i</code>, the equivalent code is <code>new StringBuilder().append("Four").append(i).toString()</code></li> <li>Autoboxing is not involved here as there is an <a href="http://docs.oracle.com/javase/6/docs/api/java/lang/StringBuilder.html#append%28int%29" rel="noreferrer">StringBuilder.append(int)</a> method which according to the docs is using <a href="http://docs.oracle.com/javase/6/docs/api/java/lang/String.html#valueOf%28int%29" rel="noreferrer">String.valueOf(int)</a> to create the string representation of the integer.</li> </ul>
    singulars
    1. This table or related slice is empty.
    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. 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.
    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