gpt4 book ai didi

java - System.out.print 语句的意外输出

转载 作者:行者123 更新时间:2023-12-04 10:59:20 25 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





String concatenation and comparison gives unexpected result in println statement

(5 个回答)


1年前关闭。




当在程序中使用 String 的实习生方法时,它给出了一些意外的输出,我无法理解为什么会给出这样的输出

public class Test {

public static void main(String[] args)
{

String str1 = "patty";
String str2 = new String("patty");

System.out.println("1 -> "+ str1 == str2);

String str3 = str2.intern();

System.out.println("2 -> "+ str1 == str3);
}
}

在输出中它显示
false
false

但预期的输出是
1   ->  false
2 -> true

谁能帮我理解这个问题

最佳答案

即使 str1str3引用相同 String例如,您不是在比较这些引用文献。

System.out.println("2   ->  "+ str1 == str3);

比较 "2 -> "+ str1str3 .

尝试:
System.out.println("2   ->  "+ (str1 == str3));

关于java - System.out.print 语句的意外输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58931924/

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