繁体   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