我的
java类
private static final String constantString = "Constant";
private static final Integer constantInteger = 5;
public static void main(String[] args) {
String s2 = constantString + "append"; // LINENUMBER 9
Integer i2 = constantInteger + 7; // LINENUMBER 10
}
字节码
LINENUMBER 9 L0
LDC "Constantappend"
ASTORE 1
L1
LINENUMBER 10 L1
GETSTATIC TestClass.constantInteger : Ljava/lang/Integer;
INVOKEVIRTUAL java/lang/Integer.intValue()I
BIPUSH 7
IADD
问题1:为什么编译器没有用5代替最终的Integer(constantInteger)值,但是对于String它做了!
如果删除Integer变量的final关键字
java代码:
private static Integer constantInteger = 5;
字节码:
LINENUMBER 10 L1
GETSTATIC TestClass.constantInteger : Ljava/lang/Integer;
INVOKEVIRTUAL java/lang/Integer.intValue()I
BIPUSH 7
字节码在两种不同情况下是相同的(静态最终整数,静态整数)
问题2:那么使Integer最终有什么用?