gpt4 book ai didi

java - 字符比较不起作用。我该怎么做呢?

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

我有一个声明 if (x[i].equals(' ')) (我将 char 数组的一个元素与空格字符进行比较,看看它是否是空格)这是抛出一个错误。所以我用谷歌搜索了一下,发现你不能用原语来做到这一点。我还发现 == 不比较值。因此,据我从我的研究中可以看出,您无法比较基元的值。这是真的?如果不是,你如何比较它们?

最佳答案

== 用于比较基元的值和引用类型的“引用值”(即查看两个引用类型变量是同一个实例,因为它们具有相同的内存地址值(value))。使用 == 进行 char 比较、int 比较、longdouble , float , ...

您可以在 Java 教程 > Equality, Relational, and Condition Operators 中阅读更多相关内容,以及 § 15.21 of the JLS ,其中指出:

15.21.1. Numerical Equality Operators == and !=

The value produced by the == operator is true if the value of the left-hand operand is equal to the value of the right-hand operand; otherwise, the result is false.

15.21.3. Reference Equality Operators == and !=

At run time, the result of == is true if the operand values are both null or both refer to the same object or array; otherwise, the result is false.

关于java - 字符比较不起作用。我该怎么做呢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28596462/

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