简体   繁体   English

未初始化的整数等于null

[英]Is not initialized integer is equivalent to null

Have few questions related to nullable type: 有几个与nullablenullable类型有关的问题:

int x;  
Console.WriteLine(x);

If a variable is not initialized does that means it contain nothing or we can say it is empty. 如果未初始化变量,则意味着它不包含任何内容,或者我们可以说它为空。 So is this empty value is equivalent to null ? 那么,这个空值等于null吗?

If yes then how a value type object contain a null value? 如果是,那么值类型对象如何包含null值? If no, then what is this empty value called? 如果否,那么这个空值叫做什么?

If I try to compile uninitialized int variable(above mentioned code) then compiler returns me an error use of unassigned local variable but it won't tell me what is stored inside the variable. 如果我尝试编译未初始化的int变量(上述代码),则编译器会错误地使用未分配的局部变量,但不会告诉我变量中存储了什么。

Unlike fields, the compiler can easily detect that you are using a local non-initialized variable, and will not let you compile that code, hence there's no value for it. 与字段不同,编译器可以轻松地检测到您正在使用本地未初始化的变量,并且不会让您编译该代码,因此没有任何价值。

With fields, the situation is a little bit different as the compiler will not shout, but rather assign the default value for that type. 对于字段,情况有些不同,因为编译器不会大喊大叫,而是为该类型分配默认值。 In case of an int it would be 0 ; 如果是int ,则为0

Java does not allow any code to read an uninitialised variable. Java不允许任何代码读取未初始化的变量。

Although the compiler/JVM (Java Virtual Machine) ensures all fields of classes are initialised to 0, 0.0, '\\0' or null (as appropriate) it cannot efficiently do that with local variables. 尽管编译器/ JVM(Java虚拟机)确保将类的所有字段都初始化为0、0.0,'\\ 0'或null(视情况而定),但是它不能使用局部变量来有效地做到这一点。 Therefore the compiler requires you to set the variable before any possible path allows it to be read. 因此,编译器要求您在任何可能的路径允许其读取之前设置变量。

A reference type is stored as a reference (like a pointer) to an object instance. 引用类型存储为对象实例的引用(如指针)。 null means a reference that isn't pointing to an instance of an object. null表示未指向对象实例的引用。

A value type is stored as the value itself, without any references. 值类型存储为值本身,没有任何引用。 It doesn't make sense to have a null value type, since by its definition it contains a value. 具有空值类型没有任何意义,因为根据其定义,它包含一个值。

int is a value type and not a reference type, which means you cannot assign a null to a value type. int是值类型,而不是引用类型,这意味着您不能为值类型分配null。

You can make a value type nullable in C# and Java (the syntax differs a bit though). 您可以在C#和Java中将值类型设置为可为空(尽管语法略有不同)。 The Nullable type has a HasValue flag that tells you whether there's no value (actually there is, but it's the default value of the value type). Nullable类型具有一个HasValue标志,该标志告诉您是否没有值(实际上是有,但是它是值类型的默认值)。 Usage (C#): 用法(C#):

int? NullableInt = null;
Console.WriteLine(NullableInt.Value);

The reason you encounter the error of use of unassigned local variable , is that local valuables aren't initialized by default. 遇到使用未分配的本地变量的错误的原因是,默认情况下未初始化本地贵重物品。 Class members are initalized by default for example. 例如,默认情况下会初始化班级成员。 They have no value and are not equal to null. 它们没有值,并且不等于null。

According to this answer , Microsoft could have allowed uninitialized local variables. 根据此答案 ,Microsoft可以允许使用未初始化的局部变量。 They simply chose not to, because it would have been a bug and not because it would prevent you from observing the garbage uninitialized state of the local. 他们只是选择不这样做,因为这可能是一个错误,而不是因为它会阻止您观察本地的垃圾未初始化状态。

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

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