Java 允许可为空类型吗?

发布于 2024-12-05 16:01:46 字数 231 浏览 1 评论 0原文

在 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技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

我不吻晚风 2024-12-12 16:01:46

不。

相反,您可以使用装箱的 Boolean 类(这是一个普通的类,而不是原始类型),或一个三值的 enum

No.

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

孤星 2024-12-12 16:01:46

你可以使用:

Boolean b = null;

,即Java中的java.lang.Boolean对象。

然后还可以通过简单的赋值设置 truefalse

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;

云朵有点甜 2024-12-12 16:01:46

不,在 java 中 基元 不能有空值,如果你想要的话此功能,您可能需要使用 布尔值 代替。

No, in java primitives cannot have null value, if you want this feature, you might want to use Boolean instead.

纸短情长 2024-12-12 16:01:46

当然,您可以使用布尔值,但为了更明显地表明您的类型可以有“值”或“无值”,创建一个或多或少执行 ? 类型的包装类是非常容易的在 C# 中执行:

public class Nullable<T> {
    private T value;
    public Nullable() { value = null; }
    public Nullable(T init) { value = init; }
    public void set(T v) { value = v; }
    public boolean hasValue() { return value != null; }
    public T value() { return value; }
    public T valueOrDefault(T defaultValue) { return value == null ? defaultValue : value; }
}

然后您可以像这样使用它:

private Nullable<Integer> myInt = new Nullable<>();
...
myInt.set(5);
...
if (myInt.hasValue()) 
   ....
int foo = myInt.valueOrDefault(10);

请注意,自 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#:

public class Nullable<T> {
    private T value;
    public Nullable() { value = null; }
    public Nullable(T init) { value = init; }
    public void set(T v) { value = v; }
    public boolean hasValue() { return value != null; }
    public T value() { return value; }
    public T valueOrDefault(T defaultValue) { return value == null ? defaultValue : value; }
}

Then you can use it like this:

private Nullable<Integer> myInt = new Nullable<>();
...
myInt.set(5);
...
if (myInt.hasValue()) 
   ....
int foo = myInt.valueOrDefault(10);

Note that something like this is standard since Java8: the Optional class.
https://docs.oracle.com/javase/8/docs/api/java/util/Optional.html

执着的年纪 2024-12-12 16:01:46

是的,你可以。

为了做到这一点,java 为每个基本类型都有一个包装类。如果将变量设置为包装类的实例,则可以像任何普通变量一样为其分配 null

而不是:

boolean myval;

... 您可以使用:

Boolean myval = null;

您可以像这样分配它:

myval = new Boolean(true);

... 并像这样获取其原始值:

if (myval.booleanValue() == false) {
  // ...
}

每个原始类型 (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:

boolean myval;

... you can use:

Boolean myval = null;

You can assign it like this:

myval = new Boolean(true);

... And get its primitive value out like this:

if (myval.booleanValue() == false) {
  // ...
}

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.

未蓝澄海的烟 2024-12-12 16:01:46

在 Java 中,基本类型不能为 null。但是,您可以使用 Boolean和朋友们。

In Java, primitive types can't be null. However, you could use Boolean and friends.

栀梦 2024-12-12 16:01:46

如果您使用对象,它允许 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

隐诗 2024-12-12 16:01:46

否,但您可以使用 Boolean 类而不是原始 boolean 类型来放置 null

No but you may use Boolean class instead of primitive boolean type to put null

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文