實際上以下兩個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
}
參見:[爲什麼人們仍然使用原語在Java中?(http://stackoverflow.com/questions/5199359/why-do-people-still-use-primitive-types-in- JAVA) – jason