java boolean空,Java检查布尔值是否为空

这篇博客探讨了如何在Java中正确检查和处理可能为null的布尔值。作者指出,由于boolean是原始类型,它不能为null,但可以使用Boolean类来允许null值。示例代码展示了如何避免因null布尔值导致的错误执行。
摘要由CSDN通过智能技术生成

How do you check if a boolean is null or not? So if I know "hideInNav" is null. How do I stop

it from further executing? Something like the below doesn't seem to work but why?

boolean hideInNav = parent.getProperties().get("hideInNav", false);

String hideNavigation = hideInNav != null ? hideInNav : "";

解决方案

boolean can only be true or false because it's a primitive datatype (+ a boolean variables default value is false). You can use the class Boolean instead if you want to use null values. Boolean is a reference type, that's the reason you can assign null to a Boolean "variable". Example:

Boolean testvar = null;

if (testvar == null) { ...}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值