2012-01-06 64 views
2

對相同類型執行顯式轉換是否會影響性能,或者它們是否被編譯器過濾並且不會達到字節碼?顯式轉換爲相同類型

實施例:

int x = 3; 
int y = (int) x; 
+0

爲什麼你需要這樣做? – 2012-01-06 09:23:52

+0

@HarryJoy我只是想知道編譯器在這種奇怪的情況下的行爲,因爲它甚至不會產生警告。 – Acidic 2012-01-06 09:26:49

+0

我真的寫過對話嗎?哈哈!感謝編輯。 – Acidic 2012-01-06 09:30:28

回答

3

運行的javap -c這個類:

public class SameTypeCastsDemo { 

    public static void withoutCasts() { 
     int x = 2; 
     int y = x; 
     System.out.println(y); 
    } 

    public static void withCast() { 
     int x = 2; 
     int y = (int) x; 
     System.out.println(y); 
    } 

} 

表明字節碼看起來相同:

public static void withoutCasts(); 
    Code: 
    0: iconst_2 
    1: istore_0 
    2: iload_0 
    3: istore_1 
    4: getstatic #2; //Field java/lang/System.out:Ljava/io/PrintStream; 
    7: iload_1 
    8: invokevirtual #3; //Method java/io/PrintStream.println:(I)V 
    11: return 

public static void withCast(); 
    Code: 
    0: iconst_2 
    1: istore_0 
    2: iload_0 
    3: istore_1 
    4: getstatic #2; //Field java/lang/System.out:Ljava/io/PrintStream; 
    7: iload_1 
    8: invokevirtual #3; //Method java/io/PrintStream.println:(I)V 
    11: return 

更新:與非基本對象的引用:

public class SameTypeCastsDemo { 
    Integer x; 
    Integer y; 

    public SameTypeCastsDemo(Integer x, Integer y) { 
     this.x = x; 
     this.y = y; 
    } 

    public void print() { 
     System.out.println(y); 
    } 

    public static void withoutCasts() { 
     SameTypeCastsDemo x = new SameTypeCastsDemo(2, 3); 
     SameTypeCastsDemo y = x; 
     y.print(); 
    } 

    public static void withCast() { 
     SameTypeCastsDemo x = new SameTypeCastsDemo(2, 3); 
     SameTypeCastsDemo y = (SameTypeCastsDemo) x; 
     y.print(); 
    } 

} 

javap -c SameTypeCastsDemo:

public static void withoutCasts(); 
    Code: 
    0: new #6; //class SameTypeCastsDemo 
    3: dup 
    4: iconst_2 
    5: invokestatic #7; //Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer; 
    8: iconst_3 
    9: invokestatic #7; //Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer; 
    12: invokespecial #8; //Method "<init>":(Ljava/lang/Integer;Ljava/lang/Integer;)V 
    15: astore_0 
    16: aload_0 
    17: astore_1 
    18: aload_1 
    19: invokevirtual #9; //Method print:()V 
    22: return 

public static void withCast(); 
    Code: 
    0: new #6; //class SameTypeCastsDemo 
    3: dup 
    4: iconst_2 
    5: invokestatic #7; //Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer; 
    8: iconst_3 
    9: invokestatic #7; //Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer; 
    12: invokespecial #8; //Method "<init>":(Ljava/lang/Integer;Ljava/lang/Integer;)V 
    15: astore_0 
    16: aload_0 
    17: astore_1 
    18: aload_1 
    19: invokevirtual #9; //Method print:()V 
    22: return 
+0

我可以認爲這也適用於對象引用? – Acidic 2012-01-06 09:34:25

1

太陽稱之爲identity conversion.

從鏈路--quote -

從類型到同樣類型A轉換允許用於任何類型。

這看起來很平凡,但它有兩個實際的結果。首先, 總是允許表達式具有以 開頭的期望類型,從而允許簡單規定的規則,即每個表達式 都可以進行轉換(如果僅進行簡單的標識轉換)。其次,這意味着爲了清晰起見,允許程序包括冗餘演員操作員。

相關問題