在String值后面打印一个int值 [英] printing an int value right after a String value
本文介绍了在String值后面打印一个int值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下示例代码:
int pay = 80;
int bonus = 65;
System.out.println(pay + bonus + " " + bonus + pay);
有人可以向我解释为什么我会得到以下输出:
could someone please explain to me why I get the following output:
145 6580
推荐答案
您的代码是从左到右解释表达式。
Your code is interpreting the expression from left to right.
-
pay + bonus
被解释为数学函数,因此它将值添加到145.+
这里是一个加号运算符。 - 当您连接
时,Java会将表达式转换为String。这里的
+
是一个连接运算符。 - 执行
+ pay
转换支付
到一个字符串并连接它,因为表达式是一个字符串。 - 还要做
+ bonus
将bonus
转换为String并连接它,也因为前一个表达式。
pay + bonus
is interpreted as a mathematical function, so it adds the values to make 145. The+
here is a plus operator.- The moment you concatenate the
" "
, Java converts the expression into a String. The+
here is a concatenate operator. - Performing
+ pay
convertspay
to a String and concatenates it, because the expression is a String. - Also doing
+ bonus
convertsbonus
to a String and concatenates it, also because of the previous expression.
这篇关于在String值后面打印一个int值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文