为什么Java在声明原始数据类型时不允许null [英] Why Java does not allow null while declaring primitive data types

查看:75
本文介绍了为什么Java在声明原始数据类型时不允许null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是对我之前的问题的继续,并且对这个问题的答案进行了解释包装类声明

This is in continuation to my previous question and accroding to answers of this question Declaration of wrapper classes

Java将原始数据类型包装到包装类然后

Java wraps primitive data type to wrapper classes then why

char c = null; // invalid
int i = null; // invalid

不允许但是

Character cObj = null; // valid
Integer iObj = null; // valid

是允许的。

推荐答案

因为基元代表,而对象变量代表复杂数据对象的引用(类似于指针)。 一般没有空值,它是一个特殊的关键词,无引用或空引用 - 这是非常不专业的答案,但我想这是最合适的。

Because primitives represent value and Object variables represent references (something like pointers) to complex data objects. There is no null value general, it is a special keyword that "references to nothing" or empty reference - this is highly unprofessional answer, but I guess it will be most appropriate.

此外,您认为什么可能是null的数值? 0? -1?但是,那些是有效整数还有什么呢?

Besides, what could be in your opinion, numerical value of null? 0? -1? But, those are valid integers so what else?

我强烈建议您开始熟悉以下复杂的java教程。您在那里解释的每个方面都在那里解释并通过示例支持。

I strongly suggest that you start familiarizing yourself with the following complex java tutorial. Every aspect that you have been asking about is explained there and supported with examples.

这篇关于为什么Java在声明原始数据类型时不允许null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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