字符串未在输出中打印。

huangapple go评论62阅读模式
英文:

String not getting printed in the output

问题

String r1 = "hello";
String r2 = "hello";
System.out.println("Result: " + (r1 == r2));

我尝试运行这段代码,但在控制台中没有得到字符串输出。唯一打印的值是r1 == r2的结果,即false。

问题在于我期望的输出是"Result: false",为什么只打印false。

我明白应该使用.equals,我只是想知道在给定的情况下出现这种行为的原因。

如果有人能指出相关文档并解释这种行为,将会很好。

英文:
String r1 = "hello";
String r2 = "hello";
System.out.println("Result: " + r1 == r2);

I tried running this code and i am not getting the String output in the console. The only value getting printed is the r1==r2 result i.e false.

The question here is I am expecting the output to be "Result: false" and why is false just getting printed.

Also i understand that .equals should be used, I just wanted to know why the result is such in the given scenario.

It would be great if some one can point to relevant documentation and help why this is the behaviour.

答案1

得分: 8

的确,输出是“false”,因为首先执行了字符串连接,所以结果是“Result: hello”,然后与r2(“hello”)进行比较,返回false。这就是为什么在控制台上看到“false”。

如果您想要看到“Result: true”,您需要使用equals而不是==,因为这是我们在Java中比较字符串的方式。请参阅下面的帖子以了解区别:
https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java

如果您真的想使用“==”进行比较,您需要添加括号,这样在连接之前会先进行比较。

String r1 = "hello";
String r2 = "hello";
System.out.println("Result: " + r1 == r2); // 输出:"false"
System.out.println("Result: " + r1.equals(r2)); // 输出:"Result: true"
System.out.println("Result: " + (r1 == r2)); // 输出:"Result: true"
英文:

Indeed the output is "false" because first the concatenation is done so it is "Result: hello" and then it is compared to r2 ("hello") which returns false.
That's why you see "false" in console.

If you want to see "Result: true" you need to use equals instead of == because that's how we compare Strings in java. See below post to understand differences:
https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java

If you really want to compare them using "==" you need to add brackets so the comparison will be first before concatenation.

    String r1 = "hello";
    String r2 = "hello";
    System.out.println("Result: " + r1 == r2); // output: "false"
    System.out.println("Result: " + r1.equals(r2)); // output: "Result: true"
    System.out.println("Result: " + (r1 == r2)); // output: "Result: true"

答案2

得分: 1

因为它被处理为

("Result: " + r1 ) == r2

这是 false。这样处理是因为 + 的优先级高于 ==

要获取字符串及结果,你需要执行以下操作

System.out.println("Result: " + (r1 == r2));

这将输出 Result: true

英文:

Because it was processed as

("Result: " + r1 ) == r2

which is false. It was processed in this way because + has a higher priority than ==.

What you need to do to get the string as well is

System.out.println("Result: " + (r1 == r2));

which will give you Result: true

答案3

得分: 1

要比较strings并以所需的方式打印,应该使用方法equals()。像这样:

String r1 = "hello";
String r2 = "hello";
System.out.println("Result: " + r1.equals(r2));
英文:

To compare strings and print the way you want you should use method equals(). Like this:

String r1 = "hello";
String r2 = "hello";
System.out.println("Result: " + r1.equals(r2));

huangapple
  • 本文由 发表于 2020年9月7日 19:17:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/63776539.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定