(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
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.
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.
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.
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.
There are two halves to the answer:
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.
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.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With