Java 允许可为空类型吗?
在 C# 中,我可以使用一个变量来允许带有问号的空值。我想要一个 true/false/null 结果。我想将其默认设置为空。布尔值将通过测试结果设置为 true/false,但有时测试不会运行,并且布尔值在 java 中默认为 false,因此用于测试的第三个选项会很好。
c# 示例:
bool? bPassed = null;
java 有类似的东西吗?
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# example:
bool? bPassed = null;
Does java have anything similar to this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
不。
相反,您可以使用装箱的
Boolean
类(这是一个普通的类,而不是原始类型),或一个三值的enum
。No.
Instead, you can use the boxed
Boolean
class (which is an ordinary class rather a primitive type), or a three-valuedenum
.你可以使用:
Boolean b = null;
,即Java中的
java.lang.Boolean
对象。然后还可以通过简单的赋值设置 true 或 false:
Boolean b = true;
或者
布尔值 b = false;
you can use :
Boolean b = null;
that is, the
java.lang.Boolean
object in Java.And then also set true or false by a simple assignment:
Boolean b = true;
or
Boolean b = false;
不,在 java 中 基元 不能有空值,如果你想要的话此功能,您可能需要使用 布尔值 代替。
No, in java primitives cannot have null value, if you want this feature, you might want to use Boolean instead.
当然,您可以使用布尔值,但为了更明显地表明您的类型可以有“值”或“无值”,创建一个或多或少执行
?
类型的包装类是非常容易的在 C# 中执行:然后您可以像这样使用它:
请注意,自 Java8 以来,类似的内容是标准的:
Optional
类。https://docs.oracle.com/javase/8 /docs/api/java/util/Optional.html
Sure you can go with Boolean, but to make it more obvious that your type can have "value" or "no value", it's very easy to make a wrapper class that does more or less what
?
types do in C#:Then you can use it like this:
Note that something like this is standard since Java8: the
Optional
class.https://docs.oracle.com/javase/8/docs/api/java/util/Optional.html
是的,你可以。
为了做到这一点,java 为每个基本类型都有一个包装类。如果将变量设置为包装类的实例,则可以像任何普通变量一样为其分配
null
。而不是:
... 您可以使用:
您可以像这样分配它:
... 并像这样获取其原始值:
每个原始类型 (
int
,boolean
,float
, ...) 具有相应的包装类型 (Integer
,Boolean
,Float
, ... )。Java 的 自动装箱 功能允许编译器有时自动将包装类型强制为其原始值,反之亦然。但是,如果编译器无法弄清楚,您始终可以手动执行此操作。
Yes you can.
To do this sort of thing, java has a wrapper class for every primitive type. If you make your variable an instance of the wrapper class, it can be assigned
null
just like any normal variable.Instead of:
... you can use:
You can assign it like this:
... And get its primitive value out like this:
Every primitive type (
int
,boolean
,float
, ...) has a corresponding wrapper type (Integer
,Boolean
,Float
, ...).Java's autoboxing feature allows the compiler to sometimes automatically coerce the wrapper type into its primitive value and vice versa. But, you can always do it manually if the compiler can't figure it out.
在 Java 中,基本类型不能为 null。但是,您可以使用
Boolean
和朋友们。
In Java, primitive types can't be null. However, you could use
Boolean
and friends.如果您使用对象,它允许
null
如果您使用原始数据类型,它不允许允许
null
Java 有 包装类
If you are using object, it allows
null
If you are using Primitive Data Types, it does not allow
null
That the reason Java has Wrapper Class
否,但您可以使用
Boolean
类而不是原始boolean
类型来放置null
No but you may use
Boolean
class instead of primitiveboolean
type to putnull