I was wondering why primitives aren't nullable.
Today I read this
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).
So I know that there are nullable primitives like int?
in C# or Integer
in Java but why aren't int's or bool's etc. directly nullable ?
This question is not directed towards any programming language but are there differences in the programming languages why they don't allow primitives to be null ?
I try to explan it with C++:
In C++ the integer, boolean,… are directly assigned to the memory, so if you create an integer variable it uses only the four bytes for the value. If you want to create a nullable integer variable you must create a pointer to the memory which stores the value, so if the pointer is null (0x0) it points to nothing. If it is not null it points to the real value of the integer.
If you want to add two integer you can do it with a single assembler-instruction, because the values can be passed directly to the processor.
This can be transferred to the bytecode of C#/Java. The primitives can be directly used by the C#/Java-Virtual Machine. All other variables cannot be used directly, they must be “dereferenced” by the compiler.