What is null in c#, java?

2020-02-09 07:39发布

问题:

Like... is it 0 like in C++? 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

回答1:

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. A reference, a pointer, and a nullable type.

The implementation of C# on the CLR represents a null reference by zero bits. (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.)

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). Sure enough, you'll get zero.

A null nullable value type is also implemented by zeroes, though in a different way. When you say

int? 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. The value is the integer zero, and the hasValue is filled with zeroes and therefore becomes false; a nullable type with the hasValue field set to false is considered to be null.

I do not know what the implementation details are on other implementations of C# / the CLI. I would be surprised if they were different; this is the obvious and sensible way to implement nulls. 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.



回答2:

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.

What matters is the behaviour of null, as defined in the language specification (see Dan's and MRFerocius' answers for details). 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.



回答3:

Java Language Specification section 4.1:

There is also a special null type, the type of the expression null, 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 be cast 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.



回答4:

From Microsoft MDSN:

The null keyword is a literal that represents a null reference, one that does not refer to any object. null is the default value of reference-type variables. Ordinary value types cannot be null. However, C# 2.0 introduced nullable value types. See Nullable Types (C# Programming Guide).



回答5:

This is null in C#



回答6:

It is a special reference ( to differentiate it from 0 ) which point to nothing.



回答7:

null is a "reference" to nothing, so it points to literally nothing.

Additional info: A C# reference type can point to null (i.e.: nothing), a value type like int cannot point to null, although a valuetype can be used in the Nullable generic reference type.



回答8:

Null is literally nothing. Some languages like PHP will equate null to 0, false, empty string, etc. under certain circumstances. Java and C# are strongly typed languages. Therefore none of those "helpful" implicit conversions take place. So null is literally null or nothing. Null is only equal to null. No other comparison will return true.



回答9:

its equivalent to

#define NULL (void*)0

in 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. 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# java null