简体   繁体   English

Java 中声明的未初始化变量会发生什么?

[英]What happens to a declared, uninitialized variable in Java?

Does it have a value?它有价值吗?

I am trying to understand what is the state of a declared but not-initialized variable/object in Java.我试图了解 Java 中已声明但未初始化的变量/对象的状态。

I cannot actually test it, because I keep getting the "Not Initialized" compile-error and I cannot seem to be able to suppress it.我实际上无法测试它,因为我不断收到“未初始化”编译错误,而且我似乎无法抑制它。

Though for example, I would guess that if the variable would be an integer it could be equal to 0 .虽然例如,我猜如果变量是一个integer它可能等于0

But what if the variable would be a String, would be it be equal to null or the isEmpty() would return true ?但是如果变量是一个字符串,它是等于null还是isEmpty()会返回true呢?

Is the value the same for all non-initialized variables?所有未初始化变量的值是否相同? or every declaration (meaning, int, string, double etc) has a different value when not explicitly initialized?或者每个声明(含义、整数、字符串、双精度等)在未显式初始化时都有不同的值?


UPDATE更新

So as I see now, it makes a big difference if the variable is declared locally or in the Class , though I seem to be unable to understand why when declaring as static in the class it gives no error, but when declaring in the main it produces the "Not Initialized" error .因此,正如我现在所看到的,如果变量是locally声明还是在Class声明会产生很大的不同,尽管我似乎无法理解为什么在类中声明为静态时它没有错误,但是在声明中时主要它产生"Not Initialized" error

How exactly a JVM does this is entirely up to the JVM and shouldn't matter for a programmer, since the compiler ensures that you do not read uninitialized local variables. JVM 究竟如何做到这一点完全取决于 JVM,对于程序员来说应该无关紧要,因为编译器确保您不会读取未初始化的局部变量。

Fields however are different.然而领域是不同的。 They need not be assigned before reading them (unless they are final ) and the value of a field that has not been assigned is null for reference types or the 0 value of the appropriate primitive type, if the field has a primitive type.在读取它们之前不需要分配它们(除非它们是final )并且未分配的字段的值为引用类型的null或适当的原始类型的0值,如果该字段具有原始类型。

Using s.isEmpty() for a field String s;s.isEmpty()用于字段String s; that has not been assigned results in a NullPointerException .尚未分配的结果将导致NullPointerException


So as I see now, it makes a big difference if the variable is declared locally or in the Class , though I seem to be unable to understand why when declaring in the class it gives no error, but when declaring in the main it produces the "Not Initialized" error.所以正如我现在看到的,如果变量是locally声明的还是在Class声明的,它会产生很大的不同,尽管我似乎无法理解为什么在类中声明时它没有给出错误,但是在 main 中声明时它会产生"Not Initialized"错误。

In general it's undesirable to work with values that do not have a value.一般来说,使用没有价值的价值是不可取的。 For this reason the language designers had 2 choices:出于这个原因,语言设计者有两个选择:

a) define a default value for variables not yet initialized a) 为尚未初始化的变量定义默认值
b) prevent the programmers from accessing the variable before writing to them. b) 防止程序员在写入变量之前访问它们。

b) is hard to achieve for fields and therefore option a) was chosen for fields. b) 字段很难实现,因此为字段选择了选项 a)。 (There could be multiple methods reading/writing that could be valid or invalid depending on the order of calls, which could only be determined at runtime). (可能有多种读/写方法可能有效或无效,具体取决于调用顺序,这只能在运行时确定)。

For local variables option b) is viable, since all possible paths of the execution of the method can be checked for assignment statements.对于局部变量,选项 b) 是可行的,因为所有可能的方法执行路径都可以检查赋值语句。 This option was chosen during the language design for local variables, since it can help to find many easy mistakes.这个选项是在局部变量的语言设计过程中选择的,因为它可以帮助发现许多简单的错误。

Fabian already provided a very clear answer, I just try to add the specification from the official documentation for reference. Fabian 已经提供了非常明确的答案,我只是尝试从官方文档中添加规范以供参考。

Fields in Class类中的字段

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 .声明但未初始化的字段将由编译器设置为合理的默认值 Generally speaking, this default will be zero or null, depending on the data type.一般而言,此默认值将为零或空值,具体取决于数据类型。 Relying on such default values, however, is generally considered bad programming style.然而,依赖这样的默认值通常被认为是糟糕的编程风格。

If not specified the default value , it only be treated as a bad style, while it's not the same case in local variables .如果未指定default value ,则仅将其视为不良样式,而在local variables则不一样。

Local Variables局部变量

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 .访问未初始化的局部变量将导致编译时错误

The default value will be based on the type of the data and place where you are using initialized variable .默认值将基于数据类型和您使用初始化变量的位置。 Please refer below for Primitive default.请参阅下面的原始默认值。

https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

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

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