Java:为什么我需要初始化一个原始局部变量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11165485/
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: Why am I required to initialize a primitive local variable?
提问by user1329572
public class Foo {
public static void main(String[] args) {
float f;
System.out.println(f);
}
}
The print statement causes the following compile-time error,
打印语句导致以下编译时错误,
The local variable f may not have been initialized
局部变量 f 可能尚未初始化
If primitives in Java already have a default value (float = 0.0f), why am I required to define one?
如果 Java 中的原语已经有一个默认值 (float = 0.0f),为什么我需要定义一个?
Edit:
编辑:
So, this works
所以,这有效
public class Foo {
float f;
public static void main(String[] args) {
System.out.println(new Foo().f);
}
}
Thanks, everyone!
感谢大家!
回答by Zakaria
Because it's a local variable. This is why nothing is assigned to it :
因为它是一个局部变量。这就是为什么没有分配给它的原因:
Local variables are slightly different; the compiler never assigns a default value to an uninitialized local variable. If you cannot initialize your local variable where it is declared, make sure to assign it a value before you attempt to use it. Accessing an uninitialized local variable will result in a compile-time error.
局部变量略有不同;编译器永远不会为未初始化的局部变量分配默认值。如果您无法在声明它的地方初始化您的局部变量,请确保在尝试使用它之前为其分配一个值。访问未初始化的局部变量将导致编译时错误。
Edit: Why does Java raise this compilation error ?If we look at the IdentifierExpression.java
class file, we will find this block :
编辑:为什么 Java 会引发此编译错误?如果我们查看IdentifierExpression.java
类文件,我们会发现这个块:
...
if (field.isLocal()) {
LocalMember local = (LocalMember)field;
if (local.scopeNumber < ctx.frameNumber && !local.isFinal()) {
env.error(where, "invalid.uplevel", id);
}
if (!vset.testVar(local.number)) {
env.error(where, "var.not.initialized", id);
vset.addVar(local.number);
}
local.readcount++;
}
...
As stated (if (!vset.testVar(local.number)) {
), the JDK checks (with testVar
) if the variable is assigned (Vset
's source codewhere we can find testVar
code). If not, it raises the error var.not.initialized
from a properties file:
如前所述 ( if (!vset.testVar(local.number)) {
),JDK 检查(使用testVar
)变量是否已分配(我们可以在其中找到代码Vset
的源testVar
代码)。如果没有,它会var.not.initialized
从属性文件中引发错误:
...
javac.err.var.not.initialized=\
Variable {0} may not have been initialized.
...
回答by sarnold
In fact, the compiler does notassign a default value to your float f
, because in this case it is a local variable -- and not a field:
事实上,编译器不会为您的 分配默认值float f
,因为在这种情况下它是一个局部变量——而不是一个字段:
Local variables are slightly different; the compiler never assigns a default value to an uninitialized local variable. If you cannot initialize your local variable where it is declared, make sure to assign it a value before you attempt to use it. Accessing an uninitialized local variable will result in a compile-time error.
局部变量略有不同;编译器永远不会为未初始化的局部变量分配默认值。如果您无法在声明它的地方初始化您的局部变量,请确保在尝试使用它之前为其分配一个值。访问未初始化的局部变量将导致编译时错误。
回答by Mike Samuel
Class fields (non-final
ones anyway) are initialized to default values. Local variables are not.
类字段(final
无论如何都不是)被初始化为默认值。局部变量不是。
It's not always necessary to assign a value when a field is declared. Fields that are declared but not initialized will be set to a reasonable default by the compiler.
声明字段时并不总是需要赋值。已声明但未初始化的字段将由编译器设置为合理的默认值。
So a (non-final
) field like f
in
因此,一个(非final
)像场f
中
class C {
float f;
}
will be initialized to 0f
but the local variable f
in
将被初始化0f
,但局部变量f
中
void myMethod() {
float f;
}
will not be.
不会是。
Local variables are treated differently from fields by the language. Local variables have a well-scoped lifetime, so any use before initialization is probably an error. Fields do not so the default initialization is often convenient.
语言对局部变量的处理与字段的处理方式不同。局部变量有一个明确的生命周期,所以在初始化之前的任何使用都可能是一个错误。字段没有所以默认初始化通常很方便。
回答by ram
Actually local variables are stored in stack.Hence there is a chance of taking any old value present for the local variable.It is a big challenge for security reason..Hence java says you have to initialise a local varible before use.
实际上局部变量存储在堆栈中。因此有可能为局部变量采用任何旧值。出于安全原因,这是一个很大的挑战..因此java说你必须在使用前初始化局部变量。
回答by user3728743
Hi guys solution is simple. the values that are stored on the heap memory are initialized by the compiler based datatype but local variables are stored on stack memory so we have to inialize it explictly.
嗨,伙计们,解决方案很简单。存储在堆内存中的值由基于编译器的数据类型初始化,但局部变量存储在堆栈内存中,因此我们必须显式初始化它。