为什么原始数据类型不能为“空"?在爪哇? [英] Why can't primitive data types be "null" in Java?

查看:26
本文介绍了为什么原始数据类型不能为“空"?在爪哇?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在声明任何原始类型数据时,例如 intdouble,它们会被初始化为 00.0.为什么我们不能将它们设置为 null?

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?

推荐答案

原始类型就是数据.另一方面,我们所说的对象只是指向数据存储位置的指针.例如:

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;

在这种情况下,object 只是一个指向 Integer 对象的指针,它的值恰好是 3.也就是说,在存储变量对象的内存位置,你所拥有的只是一个引用到数据真正所在的位置.存储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 设置为 null,但这仅意味着该对象的数据为 null(即未分配).您不能将 int 设置为 null,因为语言会将其解释为值 0.

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.

希望有帮助!

这篇关于为什么原始数据类型不能为“空"?在爪哇?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆