gpt4 book ai didi

java - 字符串和整数的评估顺序

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

  String s1 = "six" + 3 + 3;
String s2 = 3 + 3 + "six:";
System.out.println(s1);
System.out.print(s2);

输出:

  six33
6six:

为什么第一个没有加3+3,第二个加了?

最佳答案

操作顺序很重要

在第一个中,连接的工作方式如下:

String s1 = "six" + 3 + 3;
"six3" + 3 // string plus int return string
"six33" // string plus int return string

第二个:

String s2 = 3 + 3 + "six:";
6 + "six" // int plus int return int
"6six" // int plus string return string

有关更多详细信息,请阅读 Operators 的文档和 15.7. Evaluation Order

All binary operators except for the assignment operators are evaluated from left to right; assignment operators are evaluated right to left.

关于java - 字符串和整数的评估顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58903363/

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