printing an int value right after a String value

前端 未结 9 1435
日久生厌
日久生厌 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:21

    what is surrounded by " " is referred to as a 'literal print' and gets printed exactly. The "+" sign is the concatenator operator and concatenates the string with the value that is stored in the variables. pay and bonus are declared as int, but is automatically converted to a String for the purpose of printing out.

    You can print an arithmetic expression within a System.out.print statement. Use parentheses around the arithmetic expression to avoid unexpected problems.

            System.out.println("ex" + 3 + 4);   // becomes answer 34
            System.out.println("ex" + (3 + 4));   // becomes answer 7
    

提交回复
热议问题