Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is null in c#, java?

Tags:

java

c#

null

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

like image 959
argh Avatar asked Jan 08 '10 14:01

argh


People also ask

Is null the same as 0 in C?

NULL is use as an abstraction because at the time it was not clear what the value of NULL would be from system to system. So the standard value is zero, Which is the same for '0'. Using NULL or '0' you are sure your code would work on any system regardless of what their values are.

What is null used for?

A null value in a relational database is used when the value in a column is unknown or missing. A null is neither an empty string (for character or datetime data types) nor a zero value (for numeric data types).

Is 0 same as null?

The answer to that is rather simple: a NULL means that there is no value, we're looking at a blank/empty cell, and 0 means the value itself is 0. Considering there is a difference between NULL and 0, the way Tableau treats these two values therefore is different as well.

How null is defined?

Definition of null (Entry 1 of 3) 1 : having no legal or binding force : invalid a null contract. 2 : amounting to nothing : nil the null uselessness of the wireless transmitter that lacks a receiving station— Fred Majdalany. 3 : having no value : insignificant … news as null as nothing …—


1 Answers

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.

like image 78
Eric Lippert Avatar answered Sep 24 '22 01:09

Eric Lippert