gpt4 book ai didi

java - 什么时候可以使用 "=="运算符?

转载 作者:行者123 更新时间:2023-12-01 17:11:08 24 4
gpt4 key购买 nike

我找到了 jls 的引用:

The equality operators may be used to compare two operands that are convertible (§5.1.8) to numeric type, or two operands of type boolean or Boolean, or two operands that are each of either reference type or the null type. All other cases result in a compile-time error.

但是这段代码

   String str= "";
Number num = 1;
System.out.println(str == num);

每个操作数都是引用!

表示这是不兼容的类型。

jls 中哪里说这些类型应该兼容?

最佳答案

section 15.21.3 (引用相等运算符 == 和 !=):

It is a compile-time error if it is impossible to convert the type of either operand to the type of the other by a casting conversion (§5.5). The run-time values of the two operands would necessarily be unequal.

关于java - 什么时候可以使用 "=="运算符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23805024/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com