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

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

问题描述

它有价值吗?

我试图了解 Java 中已声明但未初始化的变量/对象的状态.

I am trying to understand what is the state of a declared but not-initialized variable/object in Java.

我无法实际测试它,因为我不断收到 "Not Initialized" 编译错误,而且我似乎无法抑制它.

I cannot actually test it, because I keep getting the "Not Initialized" compile-error and I cannot seem to be able to suppress it.

例如,我猜如果变量是 integer,它可能等于 0.

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

但是如果变量是一个字符串,它会等于 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?

所以正如我现在所看到的,如果在 locally 或在 Class 中声明变量会产生很大的不同,尽管在声明时我似乎无法理解为什么作为类中的静态它不会出错,但是在主声明时会产生 "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 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),并且对于引用类型或 0,尚未分配的字段的值是 null 适当的原始类型的值,如果该字段具有原始类型.

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.

对未分配的字段 String s; 使用 s.isEmpty() 会导致 NullPointerException.

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

所以正如我现在所看到的,如果在 locally 或在 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天全站免登陆