System.out.println("is"+100+5); System.out.println(100+5+"is"); System.out.println("is"+(100+5));
时间: 2024-03-05 15:51:39 浏览: 29
这段代码的输出结果分别是:
```
is1005
105is
is105
```
这是因为 Java 中的字符串拼接是从左到右依次进行的。在第一个 `println` 语句中,先将字符串 "is" 和数字 100 拼接,得到字符串 "is100",然后再与数字 5 拼接,得到最终的字符串 "is1005"。在第二个 `println` 语句中,先将数字 100 和数字 5 相加,得到数字 105,然后将其转换为字符串 "105",再与字符串 "is" 拼接,得到最终的字符串 "105is"。在第三个 `println` 语句中,由于括号的优先级比字符串拼接高,因此先计算括号中的表达式,得到数字 105,然后将其与字符串 "is" 拼接,得到最终的字符串 "is105"。
相关问题
System.out.println(5+7+"tarena"+5+7)
根据Java的运算规则,该表达式会按照从左到右的顺序进行计算和拼接。在这个表达式中,首先进行的是 `5 + 7` 的加法运算,结果是 `12`。然后,将结果 `12` 转换为字符串,接着拼接字符串 `"tarena"`,再将字符串 `"5"` 和 `"7"` 分别拼接到后面。
因此,最终的结果是字符串 `"12tarena57"`。
代码示例:
```java
System.out.println(5 + 7 + "tarena" + 5 + 7);
```
输出结果:
```
12tarena57
```
请注意,Java中的字符串拼接操作会优先进行数值计算,如果想要改变计算和拼接的顺序,可以使用括号来明确指定运算的优先级。例如:
```java
System.out.println((5 + 7) + "tarena" + (5 + 7));
```
输出结果:
```
12tarena12
```
在这个例子中,由于使用了括号,首先进行了两个加法运算得到 `12`,然后再进行字符串拼接操作,得到最终的结果为字符串 `"12tarena12"`。
System.out.println("13" + 5 + 3);
This code will result in a compilation error because there are no commas separating the three arguments in the println method. It should be written as:
System.out.println("13" + 5 + 3);
This will output the string "135" because the + operator is used for concatenation when at least one operand is a string.
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)