printing an int value right after a String value

前端 未结 9 1613
日久生厌
日久生厌 2021-01-27 06:00

I have the following example code:

int pay = 80;
int bonus = 65;
System.out.println(pay + bonus + \" \" + bonus + pay);

could someone please ex

9条回答
  •  野性不改
    2021-01-27 06:31

    Your code is interpreting the expression from left to right.

    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.

提交回复
热议问题