专业网站建设品牌,十四年专业建站经验,服务6000+客户--广州京杭网络
免费热线:400-963-0016      微信咨询  |  联系我们
位置:网站建设 > 技术支持 > Java中使用instanceof判断类型时_java
来源:网络整理     时间:2023/3/9 1:25:42    共 3614 浏览

Java中使用instanceof判断类型时?

答案是不需要,在调用instanceof之前不需进行null值判断,为什么呢?

我们来看jdk文档中的说明

At run time, the result of the instanceof operator is true if the value of the RelationalExpression is not null and the reference could be cast (§15.16) to the ReferenceType without raising a ClassCastException. Otherwise the result is false。

在运行时,只有当引用值不是null并且能够强转不抛类转换异常则返回true,否则返回false。

那么定义

Object a = null;

a instanceof Integer 返回false。

版权说明:
本网站凡注明“广州京杭 原创”的皆为本站原创文章,如需转载请注明出处!
本网转载皆注明出处,遵循行业规范,如发现作品内容版权或其它问题的,请与我们联系处理!
欢迎扫描右侧微信二维码与我们联系。
·上一条:CString如何转char_java | ·下一条:威海职业学院二级学院是什么意思_java

Copyright © 广州京杭网络科技有限公司 2005-2025 版权所有    粤ICP备16019765号 

广州京杭网络科技有限公司 版权所有