gpt4 book ai didi

java - java中ParseInt接收到的int和int的区别

转载 作者:搜寻专家 更新时间:2023-10-31 08:06:08 24 4
gpt4 key购买 nike

int i = 0;
int k = Integer.parseInt("12");
int j = k;
System.out.println(i+1 + " " + j+1);

奇怪的是收到的输出是

1 121

我无法弄清楚这个基本区别。请帮助我。

最佳答案

如下使用括号

System.out.println((i+1) + " " + (j+1));

来自docs

The + operator is syntactically left-associative, no matter whether it is later determined by type analysis to represent string concatenation or addition. In some cases care is required to get the desired result. For example, the expression:

a + b + c is always regarded as meaning: (a + b) + c

将其扩展到您的场景

i+1 + " " + j+1

变成

(((i + 1) + " ") + j)+1

因为 i 是一个 int 所以 (i + 1) = 1 ,简单的加法

"" 是一个 String 因此 ((i + 1) + "") = 1 < strong>WITH SPACE(字符串连接)

类似地,当添加 j 和最后一个 1 时,它被添加到 String 因此 String concatenation 发生了,这证明了你得到的输出。

查看

关于java - java中ParseInt接收到的int和int的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11008538/

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