Vijay Chandran
Ranch Hand
Posts: 186
Dear friends,

The above code prints
66666
66666
17777

My query is:
Why the first and second print statements printed the same value? In the first addition expression, the left operand is long type. In the second both operands are integers.

Why the result of the addition gave the same value?

Thanks,
Vijay

Vijitha Kumara
Bartender
Posts: 3916
9
Vijay Chandran wrote:Why the first and second print statements printed the same value? In the first addition expression, the left operand is long type. In the second both operands are integers.
Why the result of the addition gave the same value?

Well, what did you expect as the output ? Note that your last statement's second argument is "5432L".

Vijay Chandran
Ranch Hand
Posts: 186
Hi,

My question is:
When the first operand has a 'L' and the second operand is an integer the result of adding these 2 operands produced the same result as though the operands where integers.

But in the above problem, when the second operand has a 'L' and the first operand is an integer, it produced a different result.

Why the difference?

Thanks,
Vijay

Vijitha Kumara
Bartender
Posts: 3916
9
Vijay Chandran wrote:But in the above problem, when the second operand has a 'L' and the first operand is an integer, it produced a different result.

System.out.println(12345 + 5432L);

If so, you are adding 12345 to 5432

Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15480
43
Vijay Chandran wrote:But in the above problem, when the second operand has a 'L' and the first operand is an integer, it produced a different result.

Why the difference?

Because you are adding different numbers! 5432 is not the same number as 54321.