Java 空是什么意思?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19336608/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 16:14:05  来源:igfitidea点击:

What does null mean?

javanull

提问by St.Antario

As I understood nullis instance of Tfor all reference type T.

据我了解,nullT所有引用类型的实例T

Question 1.What's defenition of null? How does nullrepresents in memory?

问题 1.什么是防御nullnull内存中是如何表示的?

Consider the following case: We defined class

考虑以下情况:我们定义了类

public class MyClass{
    Integer i;
}

Now nullcan be cast to MyClass, but I don't explicitly define that.

Nownull可以强制转换为MyClass,但我没有明确定义。

Question 2.From what compiler will be know that nullcan be cast to MyClass?

问题 2.从哪个编译器会知道null可以转换为MyClass

采纳答案by Marko Topolnik

Formally, nullis a singleton member of the nulltype, which is defined to be the subtype of every other Java type.

形式上,null是该null类型的单例成员,它被定义为所有其他 Java 类型的子类型。

nullis a reference type and its value is the only reference value which doesn't refer to any object. Therefore there is no representation of nullin memory. The binary value of a reference-typed variable whose value is nullis simply zero (all zero bits). Even though this is not explicitly specified, it follows from the general initialization semantics of objects and any other value would cause major problems to an implementation.

null是一个引用类型,它的值是唯一不引用任何对象的引用值。因此null在内存中没有表示。引用类型变量的二进制值,其值null只是零(全零位)。尽管这没有明确指定,但它遵循对象的一般初始化语义,任何其他值都会对实现造成重大问题。

回答by nanofarad

  1. Null means nothing. No object. At all. A null value can be assigned to an object reference of any type.

  2. Null can be cast to any type as it can be assigned to an object reference of that type. It, in a way, can act as every type, except for the fact that it is useless when trying to do field/method access(where a NullPointerException will be thrown) and is notevery type. Note that null instanceof Foois always false, no matter what foois.

  1. Null没有任何意义。没有对象。在所有。可以将空值分配给任何类型的对象引用。

  2. Null 可以转换为任何类型,因为它可以分配给该类型的对象引用。在某种程度上,它可以充当每种类型,除了在尝试进行字段/方法访问时它是无用的(将抛出 NullPointerException 时)并且不是每种类型。请注意,null instanceof Foo无论是什么,总是错误的foo

回答by alfasin

nullis actually not instanceofanything!

null其实不是instanceof什么!

The instanceof operator from the Java Language Specification (§15.20.2):

Java 语言规范(第 15.20.2 节)中的 instanceof 运算符:

At run time, the result of the instanceof operator is true if the value of the RelationalExpression is not nulland the reference could be cast (§15.16) to the ReferenceType without raising a ClassCastException. Otherwise the result is false.

在运行时,如果 RelationalExpression 的值不为 null,并且可以将引用强制转换(第 15.16 节)到 ReferenceType 而不会引发 ClassCastException ,则 instanceof 运算符的结果为真。否则结果为假。

4.1. The Kinds of Types and Values

4.1. 类型和值的种类

There are two kinds of types in the Java programming language: primitive types (§4.2) and reference types (§4.3). There are, correspondingly, two kinds of data values that can be stored in variables, passed as arguments, returned by methods, and operated on: primitive values (§4.2) and reference values (§4.3).

Type: PrimitiveType ReferenceType There is also a special null type, the type of the expression null (§3.10.7, §15.8.1), which has no name.

Because the null type has no name, it is impossible to declare a variable of the null type or to cast to the null type.

The null reference is the only possible value of an expression of null type.

The null reference can always undergo a widening reference conversion to any reference type.

In practice, the programmer can ignore the null type and just pretend that null is merely a special literal that can be of any reference type.

有两种类型的Java编程语言:基本类型(4.2节)和引用类型(第4.3节)。相应地,有两种数据值可以存储在变量中、作为参数传递、由方法返回和操作:原始值(§4.2)和引用值(§4.3)。

类型:PrimitiveType ReferenceType 还有一个特殊的 null 类型,表达式 null ( §3.10.7, §15.8.1) 的类型,它没有名字。

因为 null 类型没有名字,所以无法声明 null 类型的变量或强制转换为 null 类型。

空引用是空类型表达式的唯一可能值。

空引用始终可以进行扩展引用转换为任何引用类型。

在实践中,程序员可以忽略 null 类型并假装 null 只是一个可以是任何引用类型的特殊文字。

回答by Giulio Franco

In Java, a variable is a reference to an object. A nullvalue thus indicates an unset reference (i.e. a reference to nothing).

在 Java 中,变量是对对象的引用。甲null由此值指示未设置的参考(即,参考任何操作)。

You can see variables as containers(*), inside which you can put an object of a given type, when the variable is null, it means your container is empty. It's obvious that any container can be empty (regardless of the type of object it's supposed to host), you only need to put nothing in it (or removing what was inside). Nontheless, the void is not an object of any type, it's just what's left when you take the content out of a container.

您可以将变量视为容器(*),您可以在其中放置给定类型的对象,当变量为 时null,表示您的容器是空的。很明显,任何容器都可以是空的(无论它应该承载什么类型的对象),您只需要在其中放置任何东西(或移除里面的东西)。尽管如此, void 不是任何类型的对象,它只是从容器中取出内容时剩下的东西。

(*) I do know that containers are not a good abstraction for reference values, but they were good for this specific explanation.

(*) 我确实知道容器对于参考值来说不是一个很好的抽象,但是它们对于这个特定的解释很有用。