gpt4 book ai didi

java - + 运算符和字符串

转载 作者:行者123 更新时间:2023-11-29 05:37:58 25 4
gpt4 key购买 nike

我刚开始在高中学习 AP Comp sci,我偶然发现了一个关于字符串中 + 运算符的问题

为什么System.out.println ("数字"+ 6 + 4 * 5)结果为 number620

鉴于

String s = "crunch";
int a = 3, b = 1;
System.out.print(s + a + b);
System.out.print(b + a + s);

crunch314crunch 的结果?

谢谢

最佳答案

取决于 It's precedence order

When two operators share an operand the operator with the higher precedence goes first. For example, 1 + 2 * 3 is treated as 1 + (2 * 3), whereas 1 * 2 + 3 is treated as (1 * 2) + 3 since multiplication has a higher precedence than addition (+).

关于java - + 运算符和字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18757312/

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