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

╄→гoц情女王★ 提交于 2019-12-17 02:37:09

问题


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?


回答1:


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!




回答2:


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




回答3:


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




回答4:


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.




回答5:


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




回答6:


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.




回答7:


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.




回答8:


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"



来源:https://stackoverflow.com/questions/11047276/why-cant-primitive-data-types-be-null-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!