Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what value does null really have?

(I know what null is and what its is used for)

Question: OK, say we make a reference to an object in whatever language. The computer makes a little 32-bit (or other size, depending on computer's design) space in memory for that reference. That memory can be assigned to a value that represents an object's location in memory. But when I set the reference to null, what value does it really have? (what are the individual bits in the reference set to) Are the bits just zeroed out? But wouldn't that also be a location in memory? How does the computer tell that the reference contains null instead of a reference to an object?

I know this isn't an "important" question, but I'm curious as to how it works.

Thanks guys :D

like image 850
Gordon Gustafson Avatar asked Aug 14 '09 16:08

Gordon Gustafson


People also ask

What does null mean in SQL?

In SQL, this is solved with null. It is used to signify missing or unknown values. The keyword NULL is used to indicate these values. NULL really isn’t a specific value as much as it is an indicator. Don’t think of NULL as similar to zero or blank, it isn’t the same. Zero (0) and blanks “ “, are values.

What is the difference between null and empty values?

A value of NULL is different from an empty or zero value. No two null values are equal. Comparisons between two null values, or between a NULL and any other value, return unknown because the value of each NULL is unknown. Null 1 not equals to Null 2.

What is the value of null in memory?

The value 0 (all bits at zero) is a typical value used in memory to denote null. It means that there is no value associated with name. You can also think of it as the absence of data or simply no data. Note: The actual memory value used to denote null is implementation-specific.

What is the difference between null 1 and null 2?

A value of NULL is different from an empty or zero value. No two null values are equal. Comparisons between two null values, or between a NULL and any other value, return unknown because the value of each NULL is unknown. Null 1 not equals to Null 2. No null means having no value. It is not same as 0. 0 is still a number.


2 Answers

There are two halves to the answer:

  • the value is zero (ie. all bits in the value are zero)
  • zero is never considered a valid address.

The second point is why the answer to your question "But wouldn't that also be a location in memory?" is "No" - it's simply a rule that zero is not considered a valid memory location. Attempting to access it will cause an exception.

Edit: According to Wikipedia (so it must be true 8-) "some architectures use a signed address space and use the most negative value". So it's not necessarily zero on all architectures, but whatever value it has on a given architecture, that value is considered an invalid memory location.

like image 76
RichieHindle Avatar answered Oct 12 '22 11:10

RichieHindle


In .NET, null values are represented by the "all zero" bit pattern.

This is important, as it means that correctly creating a new array or object only involves wiping the memory to create appropriate default values for all fields before starting to invoke constructors, variable initializers etc where appropriate.

(I've been trying to find where this is specified, and I'm failing so far... but it's the only implementation that makes any sense. I'll keep looking.)

like image 24
Jon Skeet Avatar answered Oct 12 '22 10:10

Jon Skeet