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

8

62

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?

Olympiaolympiad answered 15/6, 2012 at 8:40 Comment(0)
F
84

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.

Firth answered 15/6, 2012 at 8:43 Comment(1)
I suppose these best practices remain true even if Java 8 introduced optional primitives such as OptionalInt? OptionalInt.empty() could be used in place of a null value. But my understanding is Optionals should be return types of methods, and not be types for properties themselves.Coleville
C
35

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

Cuevas answered 15/6, 2012 at 8:44 Comment(0)
O
15

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).

Officialese answered 15/6, 2012 at 8:44 Comment(0)
T
12

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.

Triploid answered 15/6, 2012 at 8:42 Comment(0)
B
5

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.

Birkenhead answered 15/7, 2015 at 3:33 Comment(1)
And this is exactly the reason for the question. .NET implemented this ages ago, and Java is behind in this regard, unfortunately.Lerma
E
4

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.)

Egbert answered 15/6, 2012 at 8:43 Comment(0)
P
2

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.

Priestly answered 22/10, 2013 at 10:22 Comment(0)
A
1

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"

Amund answered 7/4, 2016 at 22:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.