简体   繁体   English

为什么原始数据类型在Java中不能为“null”?

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

When declaring any primitive type data like int or double they get initialized to 0 or 0.0 . 当声明任何基本类型数据(如intdouble它们会初始化为00.0 Why can we not set them to null ? 为什么我们不能将它们设置为null

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. 在这种情况下, object只是一个指向Integer对象的指针,该对象的值恰好是3.也就是说,在存储变量对象的内存位置,您所拥有的只是对数据实际位置的引用。 The memory position where number is stored, on the other hand, contains the value 3 directly. 另一方面,存储number的存储位置直接包含值3。

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). 因此,您可以将object设置为null,但这只是意味着该对象的数据为null(即未分配)。 You cannot set an int to null, because the language would interpret that as being the value 0. 您不能将int设置为null,因为该语言会将其解释为值0。

Hope that helps! 希望有所帮助!

Because null is a reference. 因为null是一个引用。 And primitive types are not reference types. 原始类型不是引用类型。 Only objects are reference types. 只有对象是引用类型。

Because primitive data types in Java are not Object s. 因为Java中的原始数据类型不是Object You can always use one of the wrapper classes to have an Object . 您始终可以使用其中一个包装类来拥有Object Every of the eight primitive data types has its corresponding wrapper: 八种原始数据类型中的每一种都有其相应的包装器:

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

If you are interested in the whole structure, you can start here (Primitive Data Types) . 如果您对整个结构感兴趣,可以从这里开始(原始数据类型)

Because that's what the language standard says. 因为这就是语言标准所说的。

If you want to be able to pass null , you should use the wrapper types , eg Integer instead of int . 如果您希望能够传递null ,则应使用包装器类型 ,例如Integer而不是int

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 (ie Double for double, Long for long, Boolean for boolean, etc.) 如果需要能够使用空值,则可以为每种类型使用相应的对象(即Double为double,Long为long,Boolean为布尔值等)

Objects involve more overhead than primitives. 对象涉及比基元更多的开销。 The following test shows int performs about 10x faster than Integer. 以下测试显示int的执行速度比Integer快10倍。

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. 这就是为什么.net实现了可以为空的原语,遗憾的是java没有可空的原语。

Along with all above answer i would like to add this point too. 除了以上所有答案,我还想补充这一点。

For primitive types,we have fixed memory size ie for int we have 4 bytes and char we have 2 bytes. 对于原始类型,我们有固定的内存大小,即对于int,我们有4个字节,char我们有2个字节。 And null is used only for objects because there memory size is not fixed. 并且null仅用于对象,因为内存大小不固定。

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. 与其他基本类型相同,因此null仅用于对象而不用于基本类型。

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". 首先,Primitive和Object Reference的区别在于Primitive变量存储实际值,而对象引用变量存储它们引用的对象的地址,在这种情况下在对象引用中如果没有地址则传递给“空值”。

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 = "\". 原始数据类型的默认值取决于原始数据类型:如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: EX:

Test t1 测试t1

Test t2 测试t2

(Object Wrapper Types) (对象包装类型)

Integer i 整数我

Long l 长l

Object reference default values, Jvm initializes reference variable as "null" and will also initialize array to "null" 对象引用默认值,Jvm将引用变量初始化为“null”,并且还将数组初始化为“null”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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