简体   繁体   中英

concatenating string and numbers Java

Why is the output different in these cases?

int x=20,y=10;

System.out.println("printing: " + x + y); ==> printing: 2010

System.out.println("printing: " + x * y); ==> printing: 200

Why isn't the first output 30? Is it related to operator precedence? Like first "printing" and x are concatenated and then this resulting string and y are concatenated? Am I correct?

Its the BODMAS Rule

I am showing the Order of precedence below from Higher to Low:

B  - Bracket 
O  - Power
DM - Division and Multiplication
AS - Addition and Substraction

This works from Left to Right if the Operators are of Same precedence

Now

System.out.println("printing: " + x + y);

"printing: " : Is a String"

"+" : Is the only overloaded operator in Java which will concatenate Number to String. As we have 2 "+" operator here, and x+y falls after the "printing:" + as already taken place, Its considering x and y as Strings too.

So the output is 2010.

System.out.println("printing: " + x * y);

Here the

"*" : Has higher precedence than +

So its x*y first then printing: +

So the output is 200

Do it like this if you want 200 as output in first case:

System.out.println("printing: "+ (x+y));

The Order of precedence of Bracket is higher to Addition .

Basic math tells you that adding numbers is done each at a time.

So "printing: " + x is computed first. As it sa string + int the result is "printing: 20" . Then you add y so "printing: 20" + y equals "printing: 2010" .

In the second case, multiplying is prioritary. So first x * y is calculated and equals 200 . Then "printing: " + 200 equals "printing: 200" .

The results that you observe are certainly related to operator precedence and also the order of evaluation . In the absence of another rule, ie an operator of higher precedence, operators are evaluated in order from left to right.

In the first expression, all operators have the same precedence, because they're the same operator: + , and so the first operation is evaluated. Since it involves a String , it is String concatenation, and the result is a String ; similarly for the following + .

In the second expression, one of the operators is * , which has higher precedence than + , and so is evaluated first. You get the result of the multiplication, an integer, and then the concatenation of a String and an int due to the + .

This will print 30 :

System.out.println("printing: " + (x + y))

You need the parentheses to express the precedence you wish for.

It is for operator precedence

System.out.println("printing: " + x * y);

here * operator has more precedence than + so first it calculate x * y .

System.out.println("printing: " + x + y);

Where as here all are same operator and it will be treated as String concatenation operation as there is one string value is there.

if you involve bracket into this - System.out.println("printing: " + (x + y));

Then bracket ( operator has more precedence than + so first it will calculate (x + y) and will print 30

check detail operator precedence order

In the first printing line, the + operator is applied first between the String and the int and the result is a String which is again concatenated with another int resulting a String.

In the second line, the order of the operations is: first the multiplication and the resulted int concatenated to the String.

System.out.println("2*("+a"+"+b"*"+c")")

如何打印

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