Java是否允许可以为空的类型? [英] Does Java allow nullable types?

查看:828
本文介绍了Java是否允许可以为空的类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C#中我可以使用变量来允许带有问号的空值。我想要一个true / false / null结果。我希望默认情况下将其设置为null。测试结果将布尔值设置为true / false,但有时测试不会运行,并且java中的布尔值默认为false,因此测试的第3个选项会很好。

In C# I can a variable to allow nulls with the question mark. I want to have a true/false/null result. I want to have it set to null by default. The boolean will be set to true/false by a test result, but sometimes the test is not run and a boolean is default to false in java, so 3rd option to test against would be nice.

c#例如:

bool? bPassed = null;

java有类似的东西吗?

Does java have anything similar to this?

推荐答案

否。

相反,你可以使用盒装布尔 class(普通类而不是原始类)或三值枚举

Instead, you can use the boxed Boolean class (which is an ordinary class rather a primitive type), or a three-valued enum.

这篇关于Java是否允许可以为空的类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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