需要解释:java中的三元运算符

发布于 2024-12-05 06:35:32 字数 655 浏览 0 评论 0原文

有问题的行是 return pFile.exists() ? true:空;。由于它不会引发任何编译错误,对此有何解释。它最终筹集了NPE

import java.io.File;
public class Main {
  public static void main(String... args) {
    boolean accept = accept(new File(""));
    System.out.println("accept = " + accept);
  }
  public static boolean accept(File pFile) {
    System.out.println(pFile.exists()); // prints: false, so pFile is not null
    return pFile.exists() ? true : null; //this line should throw compilation error
  }
}

pFile 不为 null;如您所见,一个 File 被实例化。但显然该文件不存在。问题不在于pFile。我对运算符如何处理 null 感兴趣。

The line in question is return pFile.exists() ? true : null;. As it does not raise any compilation error, what is the explanation for this. It ended up raising NPE.

import java.io.File;
public class Main {
  public static void main(String... args) {
    boolean accept = accept(new File(""));
    System.out.println("accept = " + accept);
  }
  public static boolean accept(File pFile) {
    System.out.println(pFile.exists()); // prints: false, so pFile is not null
    return pFile.exists() ? true : null; //this line should throw compilation error
  }
}

pFile is not null; a File is instantiated as you can see. But obviously the file is not there. The question is not about pFile. I am interested in how the operator is dealing with null.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

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

评论(3

信仰 2024-12-12 06:35:32

您的代码等效于:

public static boolean accept(File pFile) {
    System.out.println(pFile.exists()); // prints: false, so pFile is not null
    Boolean tmp = pFile.exists() ? true : null;
    return (boolean) tmp;
}

换句话说,条件运算符的类型在这种情况下为boolean,然后将其拆箱以返回boolean。当null被拆箱时,您会得到异常。

来自第15.25节 Java语言规范的第15.25节

否则,第二和第三操作数分别为S1和S2类型。令T1为将拳击转换转换为S1而产生的类型,然后将T2作为将拳击转换为S2施加的类型。条件表达式的类型是将捕获转换(§5.1.10)应用于lub(T1,T2)的结果(§15.12.2.7)。

我相信在这里适用的情况,尽管我会授予的那样清楚。

You code is equivalent to:

public static boolean accept(File pFile) {
    System.out.println(pFile.exists()); // prints: false, so pFile is not null
    Boolean tmp = pFile.exists() ? true : null;
    return (boolean) tmp;
}

On other words, the type of the conditional operator is Boolean in this case, and then the value is being unboxed to return a boolean. When null is unboxed, you get an exception.

From section 15.25 of the Java Language Specification:

Otherwise, the second and third operands are of types S1 and S2 respectively. Let T1 be the type that results from applying boxing conversion to S1, and let T2 be the type that results from applying boxing conversion to S2. The type of the conditional expression is the result of applying capture conversion (§5.1.10) to lub(T1, T2) (§15.12.2.7).

I believe that's the case that's applicable here, although I'll grant it's not as clear as it might be.

故事未完 2024-12-12 06:35:32

您从定义为返回的boolean(原始类型;注意小b)的函数中返回boolean nullnull值将自动卸载,并置于NPE。

You return Boolean null from function defined as returning boolean (a primitive type; note small b). The null value is automatically unboxed, and casues NPE.

雨的味道风的声音 2024-12-12 06:35:32

实际上,正在使用空字符串来创建文件。这将导致一个空的抽象路径名没有前缀(或目录)和一个空名称序列。因此Windows无法创建文件。反过来,这是扔npe

Actually, an empty string is being used to create a file. This results in a empty abstract pathname with no prefix(or directory) and an empty name sequence. So windows is unable to create a file. This in turn is throwing a NPE

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