Java:错误:变量可能尚未初始化
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24152351/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java: Error: variable might not have been initialized
提问by user3728000
I'm learning Java and I'm getting this error. I know this has been asked a few (a lot of) times but none of the answers seems to answer my question. The body of the code is:
我正在学习 Java,但出现此错误。我知道这已经被问过几次(很多),但似乎没有一个答案能回答我的问题。代码的主体是:
String[] number = {"too small", "one", "two", "three", "four", "too large"};
int i;
if(num<1){
i=0;
}
if(num==1){
i=1;
}
if(num==2){
i=2;
}
if(num==3){
i=3;
}
if(num==4){
i=4;
}
if(num>4){
i=5;
}
return number[i];
where the variable 'num' is declared, initialized and given previously. The error I get is: "Variable 'i' might not have been initialized" and pointing to the last line (return number[i];).
其中变量 'num' 被声明、初始化和先前给出。我得到的错误是:“变量 'i' 可能尚未初始化”并指向最后一行(返回编号 [i];)。
The thing is, if I declare 'i' and immediately assign a value (int i=0;) the code runs fine. But if I don't assign a value I get the error EVEN if a possible value is assigned after each 'if'.
问题是,如果我声明 'i' 并立即分配一个值 (int i=0;) 代码运行良好。但是,如果我不分配值,即使在每个“如果”之后分配了可能的值,也会出现错误。
I don't get this kind of error with C, for example.
例如,我不会在 C 中遇到这种错误。
Thanks
谢谢
回答by rgettman
Java doesn't analyze the logic of your if
blocks determine that one of your if
statements will run and assign a value to i
. It is simple and it sees the possibility of none of the if
statements running. In that case, no value is assigned to i
before it's used.
Java 不会分析您的if
块的逻辑,以确定您的if
语句之一将运行并将值分配给i
。它很简单,它看到没有任何if
语句运行的可能性。在这种情况下,i
在使用之前没有分配任何值。
Java will not give a default value to a local variable, even if it gives default values to class variables and instance variables. Section 4.12.5 of the JLScovers this:
Java 不会为局部变量提供默认值,即使它为类变量和实例变量提供默认值。 JLS 的第 4.12.5 节涵盖了这一点:
Every variable in a program must have a value before its value is used:
程序中的每个变量在使用它的值之前都必须有一个值:
and
和
A local variable (§14.4, §14.14) must be explicitly given a value before it is used, by either initialization (§14.4) or assignment (§15.26)
局部变量(第 14.4 节、第 14.14 节)必须在使用前通过初始化(第 14.4 节)或赋值(第 15.26 节)明确指定一个值
Assign some kind of default value to i
, when you declare it, to satisfy the compiler.
i
在声明时为 分配某种默认值以满足编译器。
int i = 0;
// Your if statements are here.
return number[i];
回答by Rogue
If you wanted to clean up the code, you could very easily do this:
如果你想清理代码,你可以很容易地做到这一点:
String[] number = {"too small", "one", "two", "three", "four", "too large"};
int i = num;
if (i < 1) { i = 0; }
if (i > 4) { i = 5; }
return number[i];
Or if the value of num
doesn't even matter:
或者,如果 的价值num
甚至无关紧要:
String[] number = {"too small", "one", "two", "three", "four", "too large"};
if (num < 1) { num = 0; }
if (num > 4) { num = 5; }
return number[num];
Even if the code you had before seemed okay logically, the compiler can't always compete with human intelligence. Giving it that default value will help to satisfy the safety of your method.
即使您之前的代码在逻辑上看起来没问题,编译器也不能总是与人类智能竞争。给它默认值将有助于满足您的方法的安全性。