简体   繁体   中英

why aren't primitives nullable

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 ?

The null value points to nothing, so it is a (non-existent) reference. In Java, Object is a reference type, therefore object-type values can hold null values (eg. Integer ). Primitives on the other hand are value types and not reference types, therefore they cannot be null .

Java has been designed this way because of performance considerations. Working with references are slower and more memory consuming than working with values.

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.

As you quoted

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.

Unlike Objects primitives hold data, and not references. Since there is no chance for primitives to hold reference in them, null has no meaning for them.

because they cant hold a null value they aren't objects only objects can hold the null value the primitive types they had there own default values for example :
char hold '\' as its default value but if you want to use a null value you should use the Character Class instead which is a container for a single char instance ... :) i hope this will help you :)

value types are structures..They can hold only values.For example Int in c# stores 1,2 and so on.We can not refer them by creating objects.

When It is considered to string or any class...They can be referred.If it is having null means not referring any thing..

Finally I want to say value types stores the value and objects stores the references of the values :)

Because types for example like int, bool etc. are value types and they must to have some value after initializing.

In C# if you initialize int like that (without assign any value):

int someInt;

in default is assigned value 0. The variable must have a specific value.

In reference types like string or instance of object:

string someString;
SomeClass someObject;

in default is assigned null ;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM