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

时间:2022-02-10 16:36:20

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?

声明任何基本类型数据(如int或double)时,它们会初始化为0或0.0。为什么我们不能将它们设置为null?

8 个解决方案

#1


46  

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.

在这种情况下,object只是一个指向Integer对象的指针,该对象的值恰好是3.也就是说,在存储变量对象的内存位置,您所拥有的只是对数据实际位置的引用。另一方面,存储数字的存储位置直接包含值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). You cannot set an int to null, because the language would interpret that as being the value 0.

因此,您可以将对象设置为null,但这只是意味着该对象的数据为null(即未分配)。您不能将int设置为null,因为该语言会将其解释为值0。

Hope that helps!

希望有所帮助!

#2


14  

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

因为null是一个引用。原始类型不是引用类型。只有对象是引用类型。

#3


9  

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:

因为Java中的原始数据类型不是对象。您始终可以使用其中一个包装类来拥有Object。八种原始数据类型中的每一种都有其相应的包装器:

  • 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


5  

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.

如果您希望能够传递null,则应使用包装器类型,例如整数而不是int。

#5


3  

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

因为它是原始类型而不是对象。如果需要能够使用空值,则可以为每种类型使用相应的对象(即Double为double,Long为long,Boolean为布尔值等)

#6


3  

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没有可空的原语。

#7


1  

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.

对于原始类型,我们有固定的内存大小,即对于int我们有4个字节,char我们有2个字节。并且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仅用于对象而不用于基本类型。

#8


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

首先,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 = "\u0000".

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

对象引用默认值,Jvm将引用变量初始化为“null”,并且还将数组初始化为“null”

#1


46  

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.

在这种情况下,object只是一个指向Integer对象的指针,该对象的值恰好是3.也就是说,在存储变量对象的内存位置,您所拥有的只是对数据实际位置的引用。另一方面,存储数字的存储位置直接包含值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). You cannot set an int to null, because the language would interpret that as being the value 0.

因此,您可以将对象设置为null,但这只是意味着该对象的数据为null(即未分配)。您不能将int设置为null,因为该语言会将其解释为值0。

Hope that helps!

希望有所帮助!

#2


14  

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

因为null是一个引用。原始类型不是引用类型。只有对象是引用类型。

#3


9  

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:

因为Java中的原始数据类型不是对象。您始终可以使用其中一个包装类来拥有Object。八种原始数据类型中的每一种都有其相应的包装器:

  • 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


5  

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.

如果您希望能够传递null,则应使用包装器类型,例如整数而不是int。

#5


3  

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

因为它是原始类型而不是对象。如果需要能够使用空值,则可以为每种类型使用相应的对象(即Double为double,Long为long,Boolean为布尔值等)

#6


3  

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没有可空的原语。

#7


1  

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.

对于原始类型,我们有固定的内存大小,即对于int我们有4个字节,char我们有2个字节。并且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仅用于对象而不用于基本类型。

#8


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

首先,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 = "\u0000".

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

对象引用默认值,Jvm将引用变量初始化为“null”,并且还将数组初始化为“null”