Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There is no difference. Any potential difference in terms of memory usage would be optimized by the compiler. </p> <p>If you compile (using <code>javap -c</code>) the two examples and compare the bytecode, you'll find that the bytecode is the same. Of course, this depends on the JVM version. But since this example is so trivial, it's probably safe to assume that neither is more memory efficient than the other.</p> <hr> <h2>Example 1:</h2> <p>Code:</p> <pre><code>public class example1 { public static void main(String[] args) { for (int i=0; i&lt;10; i++) { Object a = new Object(); } } } </code></pre> <p>Bytecode:</p> <pre><code>public class example1 extends java.lang.Object{ public example1(); Code: 0: aload_0 1: invokespecial #1; //Method java/lang/Object."&lt;init&gt;":()V 4: return public static void main(java.lang.String[]); Code: 0: iconst_0 1: istore_1 2: iload_1 3: bipush 10 5: if_icmpge 22 8: new #2; //class java/lang/Object 11: dup 12: invokespecial #1; //Method java/lang/Object."&lt;init&gt;":()V 15: astore_2 16: iinc 1, 1 19: goto 2 22: return } </code></pre> <hr> <h2>Example 2:</h2> <p>Code:</p> <pre><code>public class example2 { public static void main(String[] args) { Object a; for (int i=0; i&lt;10; i++) { a = new Object(); } } } </code></pre> <p>Bytecode:</p> <pre><code>public class example2 extends java.lang.Object{ public example2(); Code: 0: aload_0 1: invokespecial #1; //Method java/lang/Object."&lt;init&gt;":()V 4: return public static void main(java.lang.String[]); Code: 0: iconst_0 1: istore_2 2: iload_2 3: bipush 10 5: if_icmpge 22 8: new #2; //class java/lang/Object 11: dup 12: invokespecial #1; //Method java/lang/Object."&lt;init&gt;":()V 15: astore_1 16: iinc 2, 1 19: goto 2 22: return } </code></pre>
    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. 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