h4ck3d
h4ck3d

Reputation: 6364

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?

Upvotes: 18

Views: 40912

Answers (7)

Samson
Samson

Reputation: 2821

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

So "printing: " + x is computed first. As it s a 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".

Upvotes: 9

pradrep niwal
pradrep niwal

Reputation: 1

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

How to print this

Upvotes: -1

Subhrajyoti Majumder
Subhrajyoti Majumder

Reputation: 41230

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

Upvotes: 1

pb2q
pb2q

Reputation: 59637

The results that you observe are certainly related to operator precedence and also the order of evaluation. In the absence of another rule, i.e. 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 +.

Upvotes: 4

Kumar Vivek Mitra
Kumar Vivek Mitra

Reputation: 33544

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.

Upvotes: 27

Dan D.
Dan D.

Reputation: 32391

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.

Upvotes: 0

duffymo
duffymo

Reputation: 308998

This will print 30:

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

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

Upvotes: 1

Related Questions