gpt4 book ai didi

JAVA boolean 构造函数给出了意想不到的结果

转载 作者:行者123 更新时间:2023-12-01 06:49:38 24 4
gpt4 key购买 nike

我刚刚写了一段代码:

Boolean b1 = new Boolean("programmer");
Boolean b2 = new Boolean("tester");
System.out.println(b1.equals(b2));

它打印 true 作为输出。 为什么?

According to JAVA documentation

new Boolean(String): Allocates a Boolean object representing the value true if the string argument is not null and is equal, ignoring case, to the string "true".

最佳答案

Boolean b1 = new Boolean("programmer"); // false
Boolean b2 = new Boolean("tester"); // false

因此测试 false == falsetrue

<小时/>

From public Boolean(String s) constructor signature:

  • Boolean.parseBoolean("True") returns true

  • Boolean.parseBoolean("yes") returns false

关于JAVA boolean 构造函数给出了意想不到的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42367456/

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