簡體   English   中英

在定義時和定義后聲明局部變量有什么區別?

[英]What is difference between declaring local variable at the time of definition and after definition?

之間有什么區別

public static void main(String [] ar){
    int var= 10;
    System.out.println(var);
}

public static void main(String [] ar){
    int var;
    var= 10;
    System.out.println(var);
}

此外,它在Compiler / JVM中的體現是什么?

除了您使用的行數之外,沒有任何實際區別。 這是一個風格問題,對於這種簡單的情況,我將使用第一個示例。

如果代碼針對本機代碼進行了優化,則變量var可能會完全消失。

int var; //為名為var的int數據類型保留內存位置

var = 10 //將整數10分配給上述位置

您可以單獨執行這些操作,也可以按照說明一步執行。 編譯器沒有區別。

第一個示例中的語法同時執行兩個步驟,而第二個示例中的語法則分開。

    There is no at all difference between in both of the snippets mentioned other than number of lines taken to do declaration & initialization.
    The compiler will treat both of the things in the same fashion.
    Since initialization is been done before using the local variable in both the cases, there are no chances of error like 'Variable Not Initialized' or so.

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM