//ldc 123->astore_1 String a = "123"; //new->dup->ldc 1234->init->astore_2 String b = new String("1234"); //iconst_1->istore_3 int c = 1; //bipush 100->istore 4 int e = 100; //sipush 1024->invokestatic(Integer.valueOf)->astore Integer f = 1024; //new ->dup->sipush 2048->init->astore Integer g = new Integer(2048); //ldc->asotre String h = Aspect.A; //iconst_1->istore int m = Aspect.B;
从代码中可以看出:
1、字符串都会在编译时进入常量池,在使用时直接从常量池压入栈顶,
2、int类型常量会根据大小区分指令,0~5一段,-128~127一段,-32768-32767一段,其余会作为常量放入常量池
3、字符串直接赋值,是直接把常量池地址赋给变量
4、字符串新建是先新建对象,再把对象地址赋给变量
5、引用其他类的常量都会在编译期转换为本类的常量
6、自动装箱是调用Integer.valueOf