在String值后面打印一个int值 [英] printing an int value right after a String value

查看:262
本文介绍了在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.


  1. pay + bonus 被解释为数学函数,因此它将值添加到145. + 这里是一个加号运算符。

  2. 当您连接时,Java会将表达式转换为String。这里的 + 是一个连接运算符。

  3. 执行 + pay 转换支付到一个字符串并连接它,因为表达式是一个字符串。

  4. 还要做 + bonus bonus 转换为String并连接它,也因为前一个表达式。

  1. pay + bonus is interpreted as a mathematical function, so it adds the values to make 145. The + here is a plus operator.
  2. The moment you concatenate the " ", Java converts the expression into a String. The + here is a concatenate operator.
  3. Performing + pay converts pay to a String and concatenates it, because the expression is a String.
  4. Also doing + bonus converts bonus to a String and concatenates it, also because of the previous expression.

这篇关于在String值后面打印一个int值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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