A3:
Post-code and compile-generated instructions to re-analyze
public static void Main (string[] args) {
String s;
int i;
}
———-Generated Instruction ——— –
public static void Main (java.lang.string[]);
Code:
0:return
}
——-Code ———
public static void Main (string[] args) {
String S=new string ("");
int i=0;
}
-Generated instruction ———-
public static void Main (java.lang.string[]);
Code:
0:new #16; Class Java/lang/string
3:dup
4:LDC #18; String
6:invokespecial #20; Method java/lang/string. "":( Ljava/lang/str
ng;) V
9:astore_1
10:iconst_0
11:istore_2
12:return
}
As can be seen from the above, although the two variables are not used, but the compiler did not dispose of them, and allocated space for it, if the non-initialized variables will be ignored by the compiler.
Finally, add that the above Li Shouyu said variables are ignored by the compiler, I personally think this is not the case, because the new object, that is, the active use of this class, will trigger the class of some static blocks and static variables initialization, so even if not used will be compiled, Loading the JDBC driver is an example.
Local variables are not initialized when they are compiled and should be placed in the stack if initialized.
O and B, are only allocated memory in the stack, there is no memory in the heap, only new after the memory allocated in the heap.