Java中声明的未初始化变量会发生什么? [英] What happens to a declared, uninitialized variable in Java?

查看:1387
本文介绍了Java中声明的未初始化变量会发生什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

它有值吗?

我试图了解Java中声明但未初始化的变量/对象的状态是什么。

I am trying to understand what is the state of a declared but not-initialized variable/object in 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.

虽然例如,我猜如果变量是整数,它可能等于 0 即可。

Though for example, I would guess that if the variable would be an integer it could be equal to 0.

但是,如果变量是一个String,它将等于 null isEmpty()将返回 true

But what if the variable would be a String, would be it be equal to null or the isEmpty() would return 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?

正如我现在看到的,如果变量声明为 local 或<$ c,则会产生很大的不同$ c> Class ,虽然我似乎无法理解为什么在类中声明为静态时它没有给出错误,但是当在main中声明它产生Not初始化的错误

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.

推荐答案

JVM如何完全取决于JVM并且不应该对于程序员来说很重要,因为编译器确保您不会读取未初始化的本地变量。

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.

字段但是不同。在阅读它们之前不需要分配它们(除非它们是 final )并且尚未分配的字段的值是 null 用于引用类型或相应基元类型的 0 值,如果该字段具有基本类型。

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.

使用 s.isEmpty()获取字符串s; 但尚未分配的结果为 NullPointerException

Using s.isEmpty() for a field String s; that has not been assigned results in a NullPointerException.


所以我看到现在,如果变量被声明为 local 或者在 Class 中,它会产生很大的不同,尽管我似乎无法理解为什么在类中声明它没有给出任何错误,但是当在main中声明时它会产生Not Initialized错误。

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.

通常,处理没有值的值是不可取的。出于这个原因,语言设计者有两个选择:

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)为尚未初始化的变量定义默认值

b)阻止程序员访问写入之前的变量。

a) define a default value for variables not yet initialized
b) prevent the programmers from accessing the variable before writing to them.

b)很难为字段实现,因此为字段选择了选项a)。 (可能有多种读取/写入方法可能有效或无效,具体取决于调用顺序,只能在运行时确定)。

b) is hard to achieve for fields and therefore option a) was chosen for fields. (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).

对于局部变量选项b)是可行的,因为可以检查方法执行的所有可能路径的赋值语句。在本地变量的语言设计过程中选择了此选项,因为它可以帮助找到许多容易出错的错误。

For local variables option b) is viable, since all possible paths of the execution of the method can be checked for assignment statements. This option was chosen during the language design for local variables, since it can help to find many easy mistakes.

这篇关于Java中声明的未初始化变量会发生什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆