网站开发用qq登录,宝塔搭建wordpress博客,智联企业登录入口,wordpress seo怎么做本文研究的主要是java中的null“类型”的相关实例#xff0c;具体介绍如下。先给出一道简单的null相关的题目#xff0c;引发我们对null的探讨,后面会根据官方语言手册对null“类型”进行解读。题目#xff1a;下面程序能正确运行吗#xff1f;解析#xff1a;输出应该为 …本文研究的主要是java中的null“类型”的相关实例具体介绍如下。先给出一道简单的null相关的题目引发我们对null的探讨,后面会根据官方语言手册对null“类型”进行解读。题目下面程序能正确运行吗解析输出应该为 haha因为null 是可以强转为任何类类型的所以前面((NULL)null)是合法的但是null强转以后是无效对象其返回值为null,(后面会作解释)而haha方法是静态方法静态方法使用静态绑定不会抛出空指针异常。如果把haha()函数变为非静态之后将会抛出空指针异常。再来一个例子这道题其实和上面是差不多的。结果还是输出“haha”Java Language Specification中我们可以看到在4.1. The Kinds of Types and Values一节中提到There are two kinds of types in the Java programming language: primitive types (§4.2) and reference types (§4.3). ”Type:PrimitiveTypeReferenceTypeThere is also aspecial null type, the type of the expression null (§3.10.7, §15.8.1), which has no name.Because the null type has no name, it is impossible to declare a variable of the null type or to cast to the null type.The null reference is the only possible value of an expression of null type.The null reference can always undergo a widening reference conversion to any reference type.In practice, the programmer can ignore the null type and just pretend that null is merely a special literal that can be of any reference type.我给翻译一下Java语言中有两种类型一种是基本类型还有一种是引用类型。还有一个特殊的null类型即表达式null的类型它没有名字。因为null类型没有名字所以不可能声明为null类型的变量或者转换为null类型。null引用是null类型表达式唯一可能的值。null引用可以转换为任意引用类型。实际上程序员可以忽略null类型可以认为null仅仅是一个可以成为任何引用类型的特殊符号。看了这个一段就比较豁然开朗了。在5.2. Assignment Conversion一节中“A value of the null type (the null reference is the only such value) may be assigned to any reference type, resulting in a null reference of that type”一个null类型(null(空)引用是这种类型的唯一的一个值)的值可以赋值给任意类型将返回一个该类型对象的空引用(其实还是null)。在5.3. Method Invocation Conversion这一节有“A value of the null type (the null reference is the only such value) may be converted to any reference type.”即“null可以被转换为任何引用类型。”通过官方的语言手册对null类型有了非常深刻的理解。对我们编程以及以后求职笔试面试都有一定的帮助。最后希望大家遇到一些奇怪的问题除了百度外尽量多去Stack Overflow网站上去搜索也多去查官方手册。总结以上就是本文关于深入理解java中的null“类型”的全部内容希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题如有不足之处欢迎留言指出。感谢朋友们对本站的支持