gpt4 book ai didi

java - 需要有关此 Java 输出的帮助

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

这是一个非常简单的代码:

  public class Test2 {

public static void main(String[] args) {
String a = "hello2";
final String b = "hello";
String d = "hello";
String c = b + 2;
String e = d + 2;
System.out.println((a));
System.out.println((c));
System.out.println((e));
System.out.println((a == c));
System.out.println((a == e));

}
}

输出是:

hello2
hello2
hello2
true
false

请告诉我为什么最后一个是“假”?谢谢

最佳答案

public static void main(String[] args)  {
String a = "hello2";
final String b = "hello"; // this will be a compile-time constant. `final String` makes a string compile-time-constant.
String d = "hello";
String c = b + 2; // The compiler will replace b+2 by "hello2"
String e = d + 2; // d is not final. Hence value of b will be calculated at runtime.
System.out.println((a));
System.out.println((c));
System.out.println((e));
System.out.println((a == c));
System.out.println((a == e));

}

关于java - 需要有关此 Java 输出的帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32349016/

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