There is no difference. Any potential difference in memory usage will be optimized by the compiler.
If you compile (using javap -c
) two examples and compare the bytecode, you will find that the bytecode is the same. Of course, this depends on the version of the JVM. But since this example is so trivial, you can probably safely assume that none of them has more memory efficiency than the other.
Example 1:
the code:
public class example1 { public static void main(String[] args) { for (int i=0; i<10; i++) { Object a = new Object(); } } }
Bytecode:
public class example1 extends java.lang.Object{ public example1(); Code: 0: aload_0 1: invokespecial
Example 2:
the code:
public class example2 { public static void main(String[] args) { Object a; for (int i=0; i<10; i++) { a = new Object(); } } }
Bytecode:
public class example2 extends java.lang.Object{ public example2(); Code: 0: aload_0 1: invokespecial
source share