Java 变量可能未初始化错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2448843/
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
Variable might not have been initialized error
提问by David
When i try to compile this:
当我尝试编译这个时:
public static Rand searchCount (int[] x)
{
int a ;
int b ;
...
for (int l= 0; l<x.length; l++)
{
if (x[l] == 0)
a++ ;
else if (x[l] == 1)
b++ ;
}
...
}
I get these errors:
我收到这些错误:
Rand.java:72: variable a might not have been initialized
a++ ;
^
Rand.java:74: variable b might not have been initialized
b++ ;
^
2 errors
it seems to me that i initialized them at the top of the method. Whats going wrong?
在我看来,我在方法的顶部初始化了它们。怎么了?
采纳答案by mipadi
You declaredthem, but you didn't initialize them. Initializing them is setting them equal to a value:
你声明了它们,但你没有初始化它们。初始化它们是将它们设置为等于一个值:
int a; // This is a declaration
a = 0; // This is an initialization
int b = 1; // This is a declaration and initialization
You get the error because you haven't initialized the variables, but you increment them (e.g., a++
) in the for
loop.
你得到错误是因为你没有初始化变量,但是你a++
在for
循环中增加了它们(例如,)。
Java primitives have default values but as one user commented below
Java 原语具有默认值,但正如一位用户在下面评论的那样
Their default value is zero when declared as class members. Local variables don't have default values
声明为类成员时,它们的默认值为零。局部变量没有默认值
回答by Jerome
You declared them, but didn't initialize them with a value. Add something like this :
你声明了它们,但没有用值初始化它们。添加如下内容:
int a = 0;
回答by Andy Shellam
You haven't initialised a
and b
, only declared them. There is a subtle difference.
你还没有初始化a
and b
,只是声明了它们。有一个微妙的区别。
int a = 0;
int b = 0;
At least this is for C++, I presume Java is the same concept.
至少这是针对 C++ 的,我认为 Java 是相同的概念。
回答by Thomas Owens
You declared them at the start of the method, but you never initialized them. Initializing would be setting them equal to a value, such as:
您在方法开始时声明了它们,但从未初始化它们。初始化会将它们设置为等于一个值,例如:
int a = 0;
int b = 0;
回答by Roman
If they were declared as fields of the class then they would be really initialized with 0.
如果它们被声明为类的字段,那么它们将真正被初始化为 0。
You're a bit confused because if you write:
你有点困惑,因为如果你写:
class Clazz {
int a;
int b;
Clazz () {
super ();
b = 0;
}
public void printA () {
sout (a + b);
}
public static void main (String[] args) {
new Clazz ().printA ();
}
}
Then this code will print "0". It's because a special constructor will be called when you create new instance of Clazz. At first super ()
will be called, then field a
will be initialized implicitly, and then line b = 0
will be executed.
然后此代码将打印“0”。这是因为当您创建新的 Clazz 实例时会调用一个特殊的构造函数。首先super ()
将被调用,然后字段a
将被隐式初始化,然后行将b = 0
被执行。
回答by Konrad Garus
You declared them, but not initialized.
你声明了它们,但没有初始化。
int a; // declaration, unknown value
a = 0; // initialization
int a = 0; // declaration with initialization
回答by Bob Jarvis - Reinstate Monica
You declared them but did not provide them with an intial value - thus, they're unintialized. Try something like:
您声明了它们但没有为它们提供初始值 - 因此,它们是未初始化的。尝试类似:
public static Rand searchCount (int[] x)
{
int a = 0 ;
int b = 0 ;
and the warnings should go away.
并且警告应该消失。
回答by codymanix
Imagine what happens if x[l] is neither 0 nor 1 in the loop. In that case a and b will never be assigned to and have an undefined value. You must initialize them both with some value, for example 0.
想象一下如果 x[l] 在循环中既不是 0 也不是 1 会发生什么。在这种情况下, a 和 b 将永远不会被分配并具有未定义的值。您必须用某个值初始化它们,例如 0。
回答by reddy
Local variables do not get default values. Their initial values are undefined with out assigning values by some means. Before you can use local variables they must be initialized.
局部变量没有默认值。它们的初始值是未定义的,没有通过某种方式分配值。在你可以使用局部变量之前,它们必须被初始化。
There is a big difference when you declare a variable at class level (as a member ie. as a field) and at method level.
在类级别(作为成员,即作为字段)和方法级别声明变量时有很大的不同。
If you declare a field at class level they get default values according to their type. If you declare a variable at method level or as a block (means anycode inside {}) do not get any values and remain undefined until somehow they get some starting values ie some values assigned to them.
如果您在类级别声明一个字段,它们将根据其类型获得默认值。如果您在方法级别或作为块(意味着 {} 内的任何代码)声明变量,则不会获得任何值并保持未定义状态,直到它们以某种方式获得一些起始值,即分配给它们的某些值。
回答by Rishabh Agarwal
Set variable "a" to some value like this,
将变量“a”设置为这样的某个值,
a=0;
Declaring and initialzing are both different.
声明和初始化都是不同的。
Good Luck
祝你好运