Why can't primitive data types be "null" in Java?

JavaNullPrimitive Types

Java Problem Overview


When declaring any primitive type data like int or double they get initialized to 0 or 0.0. Why can we not set them to null?

Java Solutions


Solution 1 - Java

A primitive type is just data. What we call objects, on the other hand, are just pointers to where the data is stored. For example:

Integer object = new Integer(3);
int number = 3;

In this case, object is just a pointer to an Integer object whose value happens to be 3. That is, at the memory position where the variable object is stored, all you have is a reference to where the data really is. The memory position where number is stored, on the other hand, contains the value 3 directly.

So, you could set the object to null, but that would just mean that the data of that object is in null (that is, not assigned). You cannot set an int to null, because the language would interpret that as being the value 0.

Hope that helps!

Solution 2 - Java

Because null is a reference. And primitive types are not reference types. Only objects are reference types.

Solution 3 - Java

Because primitive data types in Java are not Objects. You can always use one of the wrapper classes to have an Object. Every of the eight primitive data types has its corresponding wrapper:

  • byte: java.lang.Byte
  • short: java.lang.Short
  • int: java.lang.Integer
  • long: java.lang.Long
  • float: java.lang.Float
  • double: java.lang.Double
  • boolean: java.lang.Boolean
  • char java.lang.Character

If you are interested in the whole structure, you can start here (Primitive Data Types).

Solution 4 - Java

Because that's what the language standard says.

If you want to be able to pass null, you should use the wrapper types, e.g. Integer instead of int.

Solution 5 - Java

Because it is a primitive type and not an object. You can use the corresponding object for each type if you need the ability to use null values (i.e. Double for double, Long for long, Boolean for boolean, etc.)

Solution 6 - Java

Objects involve more overhead than primitives. The following test shows int performs about 10x faster than Integer.

int n;
EtmPoint point1 = etmMonitor.createPoint("test:objects");
for (n = 0; n < 1000000; n++) {
    Integer t = 0;
    t = 10;
    t = 11;
}

point1.collect();
EtmPoint point = etmMonitor.createPoint("test:primitives");
for (n = 0; n < 1000000; n++) {
    int t = 0;
    t = 10;
    t = 11;
}
point.collect();

etmMonitor.render(new SimpleTextRenderer());

This is why .net implemented nullable primitives, unfortunately java does not have nullable primitives.

Solution 7 - Java

Along with all above answer i would like to add this point too.

For primitive types,we have fixed memory size i.e for int we have 4 bytes and char we have 2 bytes. And null is used only for objects because there memory size is not fixed.

So by default we have,

   int a=0;

and not

   int a=null;

Same with other primitive types and hence null is only used for objects and not for primitive types.

Solution 8 - Java

First of all, The difference of Primitive and Object Reference is Primitive variable store the actual values, Whereas object reference variable store the the address of the object they refer to, in this case in object reference if there is no address it will pass to "null".

Default values of Primitive data type depends on the primitive data type: like byte = 0, short = 0, int = 0, long = 0L, float = 0.0f, double = 0.0d, boolean = false, char = "\u0000".

When we declare a variable of any class type, it is known as reference data type.

EX:

Test t1

Test t2

(Object Wrapper Types)

Integer i

Long l

Object reference default values, Jvm initializes reference variable as "null" and will also initialize array to "null"

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMBMJView Question on Stackoverflow
Solution 1 - JavaMiquelView Answer on Stackoverflow
Solution 2 - JavaPolygnomeView Answer on Stackoverflow
Solution 3 - JavabrimboriumView Answer on Stackoverflow
Solution 4 - JavaOliver CharlesworthView Answer on Stackoverflow
Solution 5 - JavaduduamarView Answer on Stackoverflow
Solution 6 - JavaChrisView Answer on Stackoverflow
Solution 7 - JavaShoaib ChikateView Answer on Stackoverflow
Solution 8 - JavaYelView Answer on Stackoverflow