简体   繁体   中英

printing an int value right after a String value

I have the following example code:

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

Because, this is operator overloading issue. Here, First + is plus operator and last + is concat operator.

 System.out.println(pay + bonus + " " + bonus + pay);
                        |                     |
                      (plus)                (concat)

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.

First it adds the two variables and at last it concatinates as string because the integers are converted into strings

For concatenation, imagine a and b are integers:

"" + a + b

This works because the + operator is overloaded if either operand is a String. It then converts the other operand to a string (if needed) and results in a new concatenated string. You could also invoke Integer.toString(a) + Integer.toString(b) for concatenation

bonus and pay are both ints, and therefore going to be combined into a single int result.

You need to insert an empty string between them.

首先是plus operator ,最后是concat operator

As the others are saying the compiler is first adding the integer values and then printing the result, after " " the total value is changed to String type and after that + operator is functioning as a concat action. To not get that output, you can do this:

System.out.println(String.valueOf(pay) + String.valueOf(bonus) + " " + String.valueOf(bonus) + String.valueOf(pay));

The 1st pay and bonus in the println returns an integer. So it computes pay+bonus and returns it as an integer before printing it out.

However, after the "". The + operation then becomes a concatenation of strings and everything after that is returned as a concatenated string. Hence, ("" + bonus + pay) would be returned as "6580".

在“”之前,将奖金和奖金作为整数,相加的结果是145。在“”之后,将奖金和奖金作为String的结果是“ 6580”

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM