java中ParseInt收到的int和int之间的区别 [英] Difference between int and int received by ParseInt in java

查看:197
本文介绍了java中ParseInt收到的int和int之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

奇怪的是收到的输出是

1 121

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

I can not figure out this basic difference. Please help me.

推荐答案

使用括号如下

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始终被视为含义:(a + b)+ c

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

将此扩展到您的场景

i+1 + " " + j+1

它变为

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

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

Since i is an int so (i + 1) = 1 , simple addition

字符串因此((i + 1)+) = 1 WITH SPACE (String 连接

" " is a String hence ((i + 1) + " ") = 1 WITH SPACE (String concatenation)

类似地,当 j 并且最后 1 已添加,它被添加到字符串因此字符串 连接发生,这证明你得到的输出是合理的。

Similarly when j and last 1 is added, its being added to a String hence String concatenation takes place, which justifies the output that you are getting.

参见

  • JLS 15.18.1 String Concatenation Operator +

这篇关于java中ParseInt收到的int和int之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆