可以从null转换为int吗? [英] Conversion from null to int possible?

查看:435
本文介绍了可以从null转换为int吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道当我读到这个答案时,我会发现我忽略了一些在我眼前的b $ b。但是我花了最后30分钟试图弄清楚自己
没有结果。

I know that when I read the answer to this I will see that I have overlooked something that was under my eyes. But I have spent the last 30 minutes trying to figure it out myself with no result.

所以,我正在用Java 6编写一个程序并发现了一些(对我来说)奇怪的功能。
为了尝试隔离它,我做了两个小例子。
我首先尝试了以下方法:

So, I was writing a program in Java 6 and discovered some (for me) strange feature. In order to try and isolate it, I have made two small examples. I first tried the following method:

private static int foo()
{
    return null;
}

并且编译器拒绝它:类型不匹配:无法从null转换为int。

and the compiler refused it: Type mismatch: cannot convert from null to int.

这对我很好,它尊重我熟悉的Java语义。
然后我尝试了以下内容:

This is fine with me and it respects the Java semantics I am familiar with. Then I tried the following:

private static Integer foo(int x)
{
    if (x < 0)
    {
        return null;
    }
    else
    {
        return new Integer(x);
    }
}

private static int bar(int x)
{
    Integer y = foo(x);

    return y == null ? null : y.intValue();
}

private static void runTest()
{
    for (int index = 2; index > -2; index--)
    {
        System.out.println("bar(" + index + ") = " + bar(index));
    }
}

这个编译没有错误!但是,在我看来,行中应该有一个类型转换错误

This compiles with no errors! But, in my opinion, there should be a type conversion error in the line

    return y == null ? null : y.intValue();

如果我运行程序,我会得到以下输出:

If I run the program I get the following output:

bar(2) = 2
bar(1) = 1
bar(0) = 0
Exception in thread "main" java.lang.NullPointerException
    at Test.bar(Test.java:23)
    at Test.runTest(Test.java:30)
    at Test.main(Test.java:36)

你能解释一下这种行为吗?

Can you explain this behaviour?

更新

非常感谢您提供了许多澄清答案。我有点担心,因为
这个例子并不符合我的直觉。令我不安的一件事是
,将null转换为int,我想知道
的结果是什么:0就像在C ++中一样?那会很奇怪。
好​​在运行时无法进行转换(空指针异常)。

Thank you very much for the many clarifying answers. I was a bit worried because this example did not correspond to my intuition. One thing that disturbed me was that a null was being converted to an int and I was wondering what the result would be: 0 like in C++? That would hae been very strange. Good that the conversion is not possible at runtime (null pointer exception).

推荐答案

让我们来看看这一行:

Let's look at the line:

return y == null ? null : y.intValue();

中? :语句,的两边必须具有相同的类型。在这种情况下,Java将使其具有类型 Integer 整数可以是 null ,所以左侧是正常的。表达式 y.intValue()的类型为 int ,但Java将自动将其设置为整数(注意,你也可以编写 y 这可以为你保存这个autobox)。

In a ? : statement, both sides of the : must have the same type. In this case, Java is going to make it have the type Integer. An Integer can be null, so the left side is ok. The expression y.intValue() is of type int, but Java is going to auto-box this to Integer (note, you could just as well have written y which would have saved you this autobox).

现在,结果必须再次取消装箱到 int ,因为方法的返回类型是 int 。如果您取消整数 null ,则会得到 NullPointerException

Now, the result has to be unboxed again to int, because the return type of the method is int. If you unbox an Integer that is null, you get a NullPointerException.

注意:第15.25段解释了与相关的类型转换的确切规则? :条件运算符。

Note: Paragraph 15.25 of the Java Language Specification explains the exact rules for type conversions with regard to the ? : conditional operator.

这篇关于可以从null转换为int吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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