简体   繁体   English

什么是c#,java中的null?

[英]What is null in c#, java?

Like... is it 0 like in C++? 就像......在C ++中它是0吗? Or is it some "special" object? 还是一些“特殊”的对象? Or maybe something totally different? 或者也许完全不同的东西?

-- EDIT -- - 编辑 -

I do know what it is , the question is rather - how it's done 我知道它是什么 ,问题是 - 如何完成的

Since you're asking about implementation details, rather than semantics, the answer is specific to a given implementation. 由于您询问的是实现细节而不是语义,因此答案特定于给定的实现。

There are three things in C# that "null" can be. C#中有三个“null”可以。 A reference, a pointer, and a nullable type. 引用,指针和可空类型。

The implementation of C# on the CLR represents a null reference by zero bits. CLR上的C#实现表示零位的空引用 (Where the number of bits is the appropriate size to be a managed pointer on the particular version of the CLR that you're running.) (其中位数是适合您正在运行的CLR的特定版本上的托管指针的大小。)

Unsurprisingly, a null pointer is represented the same way. 不出所料,空指针以相同的方式表示。 You can demonstrate this in C# by making an unsafe block, making a null pointer to void, and then converting that to IntPtr, and then converting the IntPtr to int (or long, on 64 bit systems). 您可以在C#中通过创建一个不安全的块,使空指针变为void,然后将其转换为IntPtr,然后将IntPtr转换为int(或64位系统上的long)来证明这一点。 Sure enough, you'll get zero. 果然,你会得到零。

A null nullable value type is also implemented by zeroes, though in a different way. null 可空值类型也由零实现,但方式不同。 When you say 当你说

int? 诠释? j = null; j = null;

what we actually create is the moral equivalent of: 我们实际创造的是道德等同于:

struct NullableInt 
{
    int value;
    bool hasValue;
}

With appropriate accessor methods, and so on. 使用适当的访问方法等。 When one of those things is assigned null, we just fill the whole thing with zero bits. 当其中一个东西被赋值为null时,我们只用零位填充整个东西。 The value is the integer zero, and the hasValue is filled with zeroes and therefore becomes false; 该值为整数零,并且hasValue用零填充,因此变为false; a nullable type with the hasValue field set to false is considered to be null. hasValue字段设置为false的可空类型被视为null。

I do not know what the implementation details are on other implementations of C# / the CLI. 我不知道C#/ CLI的其他实现的实现细节是什么。 I would be surprised if they were different; 如果他们不同,我会感到惊讶; this is the obvious and sensible way to implement nulls. 这是实现null的明显而明智的方法。 But if you have questions about a specific implementation detail, you'll have to ask someone who knows about the implementation you're interested in. 但是如果您对特定的实现细节有疑问,那么您将不得不问一个了解您感兴趣的实现的人。

Since Java and C# run on virtual machines, it does not matter what is used physically to represent null, and it is not necessarily the same across implementations. 由于Java和C#在虚拟机上运行,​​因此在物理上使用什么来表示null并不重要,并且在实现之间不一定相同。

What matters is the behaviour of null, as defined in the language specification (see Dan's and MRFerocius' answers for details). 重要的是null的行为 ,如语言规范中所定义的(有关详细信息,请参阅Dan和MRFerocius的答案)。 Basically, it is a special value that variables of reference type can hold, and which cannot be dereferenced. 基本上,它是一个特殊值,引用类型的变量可以容纳,并且不能解除引用。

BTW, as a reference point, the Java serialization spec use a single byte value 0x70 to represent a null reference. BTW,作为参考点, Java序列化规范使用单字节值0x70来表示空引用。

Java Language Specification section 4.1 : Java语言规范第4.1节

There is also a special null type, the type of the expression null, which has no name. 还有一个特殊的null类型,表达式的类型为null,没有名称。 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. 由于null类型没有名称,因此无法声明null类型的变量或转换为null类型。 The null reference is the only possible value of an expression of null type. 空引用是null类型表达式的唯一可能值。 The null reference can always be cast to any reference type. null引用始终可以强制转换为任何引用类型。 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. 实际上,程序员可以忽略null类型,只是假装null只是一个可以是任何引用类型的特殊文字。

From Microsoft MDSN: 来自Microsoft MDSN:

The null keyword is a literal that represents a null reference, one that does not refer to any object. null关键字是表示空引用的文字,不引用任何对象。 null is the default value of reference-type variables. null是引用类型变量的默认值。 Ordinary value types cannot be null. 普通值类型不能为空。 However, C# 2.0 introduced nullable value types. 但是,C#2.0引入了可空值类型。 See Nullable Types (C# Programming Guide). 请参阅Nullable Types(C#编程指南)。

它是一个特殊的参考(区别于0),它指向任何东西。

null is a "reference" to nothing, so it points to literally nothing. null是对任何东西的“引用”,因此它几乎没有指向任何东西。

Additional info: AC# reference type can point to null (ie: nothing), a value type like int cannot point to null, although a valuetype can be used in the Nullable generic reference type. 附加信息:AC#引用类型可以指向null(即:nothing),类似int的值类型不能指向null,尽管可以在Nullable泛型引用类型中使用valuetype。

its equivalent to 它相当于

#define NULL (void*)0

in c++. 在c ++中。 So basically, yes, it is zero. 所以基本上,是的,它是零。

EDIT: Since I apparently didn't word this answer anywhere near correctly... 编辑:因为我显然没有正确地说出这个答案...

What I meant is this: 我的意思是:

As C# runs in a VM, what Michael Borgwardt said is correct, we don't know how its represented behind the scenes. 由于C#在VM中运行,Michael Borgwardt所说的是正确的,我们不知道它在幕后的表现如何。 However, if you take the following code: 但是,如果您使用以下代码:

    unsafe static void Main(string[] args)
    {
        if (((void*)0) == null)
            Console.WriteLine("true");
        else
            Console.WriteLine("false");

        Console.ReadKey();
    }

and compile it in a console app, with "Allow unsafe code" enabled, you will see that in c#, null is indeed equal to (void*) 0. 并在控制台应用程序中编译它,启用“允许不安全代码”,您将看到在c#中,null确实等于(void *)0。

Null is literally nothing. Null几乎没有。 Some languages like PHP will equate null to 0, false, empty string, etc. under certain circumstances. 在某些情况下,某些语言(如PHP)会将null等同为0,false,空字符串等。 Java and C# are strongly typed languages. Java和C#是强类型语言。 Therefore none of those "helpful" implicit conversions take place. 因此,没有任何“有用的”隐式转换发生。 So null is literally null or nothing. 所以null实际上是null或什么都不是。 Null is only equal to null. Null仅等于null。 No other comparison will return true. 没有其他比较会返回真实。

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

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