实际上,以下两个 java 应用程序生成相同的二进制代码(使用 JDK 7 u25 x64):
ValueOfDemo.java
public class ValueOfDemo
{
public static void main(String[] args)
{
float a = Float.valueOf("1.5").floatValue();
}
}
编译:
javac ValueOfDemo.java
拆卸:
javap -c -cp . ValueOfDemo
public class ValueOfDemo {
public ValueOfDemo();
Code:
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: return
public static void main(java.lang.String[]);
Code:
0: ldc #2 // String 1.5
2: invokestatic #3 // Method java/lang/Float.valueOf:(Ljava/lang/String;)Ljava/lang/Float;
5: invokevirtual #4 // Method java/lang/Float.floatValue:()F
8: fstore_1
9: return
}
ValueOfDemo2.java
public class ValueOfDemo2
{
public static void main(String[] args)
{
float a = Float.valueOf("1.5");
}
}
编译:
javac ValueOfDemo2.java
拆卸:
javap -c -cp . ValueOfDemo2
public class ValueOfDemo2 {
public ValueOfDemo2();
Code:
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: return
public static void main(java.lang.String[]);
Code:
0: ldc #2 // String 1.5
2: invokestatic #3 // Method java/lang/Float.valueOf:(Ljava/lang/String;)Ljava/lang/Float;
5: invokevirtual #4 // Method java/lang/Float.floatValue:()F
8: fstore_1
9: return
}