Java check if boolean is null(Java 检查布尔值是否为空)
问题描述
如何检查布尔值是否为空?因此,如果我知道hideInNav"为空.我该如何停止它从进一步执行?像下面这样的东西似乎不起作用,但为什么呢?
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
只能是 true
或 false
因为它是一个 原始数据类型(+一个 boolean
变量默认值值为 false
).如果你想使用 null
值,你可以使用 Boolean
类.Boolean 是一种引用类型,这就是您可以将 null
分配给布尔变量"的原因.示例:
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) { ...}
这篇关于Java 检查布尔值是否为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Java 检查布尔值是否为空


- value & 是什么意思?0xff 在 Java 中做什么? 2022-01-01
- Jersey REST 客户端:发布多部分数据 2022-01-01
- Java包名称中单词分隔符的约定是什么? 2022-01-01
- Spring Boot连接到使用仲裁器运行的MongoDB副本集 2022-01-01
- 将log4j 1.2配置转换为log4j 2配置 2022-01-01
- 从 finally 块返回时 Java 的奇怪行为 2022-01-01
- Eclipse 插件更新错误日志在哪里? 2022-01-01
- C++ 和 Java 进程之间的共享内存 2022-01-01
- Safepoint+stats 日志,输出 JDK12 中没有 vmop 操作 2022-01-01
- 如何使用WebFilter实现授权头检查 2022-01-01