简体   繁体   English

未初始化的局部变量 - 没有错误:Java

[英]Uninitialized local variable - No error : Java

Here are two uninitialized local variables.这是两个未初始化的局部变量。 Still this does not give compile time or runtime error and executes completely.尽管如此,这不会产生编译时间或运行时错误并完全执行。 Is this thing permissible in Java and How (explanation will be welcome).这件事在 Java 和 How 中是否允许(欢迎解释)。

class A2{ }

public class A {
public static void main(String[] args) {
    int x;
    A2 a2;

    System.out.println("Main");
}

}

There's nothing incorrect about that code.该代码没有任何不正确之处。 You don't actually use those variables so there's no issue.您实际上并未使用这些变量,因此没有问题。 If you did try to use them it would then become a problem.如果您确实尝试使用它们,则会成为问题。 For example,例如,

System.out.println(a2);
System.out.println(x);

would bring about "Variable 'x'/'a2' might not have been intitialized" errors.会导致“变量 'x'/'a2' 可能尚未初始化”错误。 There will be no default values or ability to run the code.将没有默认值或运行代码的能力。 It will be a compile time error and your code won't run.这将是一个编译时错误,您的代码将无法运行。 If the variables were class fields they would get default values for certain types or null otherwise.如果变量是类字段,它们将获得某些类型的默认值,否则为null

Both variables are not used in code.这两个变量都没有在代码中使用。 Once you try to use it as System.out.println("Main" + x);一旦您尝试将其用作 System.out.println("Main" + x); It will give you compilation error as local variable is not initialized.它会给你编译错误,因为局部变量没有初始化。

Yes, local variable declarations you have done in the above code is allowed as long as you don't access them.是的,只要您不访问它们,您在上述代码中所做的局部变量声明是允许的。 If you happen to write any code that access those variables, the code will not compile.如果您碰巧编写了访问这些变量的任何代码,则该代码将无法编译。

According to Java Language Specification, you can't access local variables (the variables that you declare inside a method) unless they are initialized before accessing.根据 Java 语言规范,您不能访问局部变量(您在方法中声明的变量),除非它们在访问之前被初始化。 Below is from Java Language Specification for SE 8.以下来自 SE 8 的 Java 语言规范。

https://docs.oracle.com/javase/specs/jls/se8/html/jls-14.html#jls-14.4.1 https://docs.oracle.com/javase/specs/jls/se8/html/jls-14.html#jls-14.4.1

Chapter 16 - Definite Assignment第 16 章 - 明确分配

"For every access of a local variable or blank final field x, x must be definitely assigned before the access, or a compile-time error occurs." “对于局部变量或空白最终字段 x 的每次访问,必须在访问之前明确分配 x,否则会发生编译时错误。”

Chapter 14第14章

14.4.2 Execution of Local Variable Declarations A local variable declaration statement is an executable statement. 14.4.2 局部变量声明的执行局部变量声明语句是一个可执行语句。 Every time it is executed, the declarators are processed in order from left to right.每次执行时,声明符都会按从左到右的顺序进行处理。 If a declarator has an initializer, the initializer is evaluated and its value is assigned to the variable.如果声明符有一个初始化器,初始化器被求值并将其值赋给变量。 If a declarator does not have an initializer, then every reference to the variable must be preceded by execution of an assignment to the variable, or a compile-time error occurs by the rules of §16 (Definite Assignment).如果声明符没有初始值设定项,则每次对变量的引用都必须先执行对变量的赋值,否则会根据 §16(确定赋值)的规则发生编译时错误。 Each initializer (except the first) is evaluated only if evaluation of the preceding initializer completes normally.每个初始化器(第一个除外)仅在前一个初始化器的评估正常完成时才被评估。 Execution of the local variable declaration completes normally only if evaluation of the last initializer completes normally.仅当最后一个初始化程序的评估正常完成时,局部变量声明的执行才能正常完成。 If the local variable declaration contains no initializers, then executing it always completes normally.如果局部变量声明不包含初始值设定项,则执行它总是正常完成。

you will get a compile-time error once you start using uninitialized local variables.一旦开始使用未初始化的局部变量,就会出现编译时错误。 For example:例如:

Case 1: No error案例 1:没有错误

public static void doJob(int[] a){
    
   int temp;   
}

Case 2: Gives error案例 2:给出错误

 public static void doJob(int[] a){
    
    int temp;
    System.out.println(temp);    
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM