wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Addition problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Addition problem" Watch "Addition problem" New topic
Author

Addition problem

Vijay Chandran
Ranch Hand

Joined: Jan 07, 2007
Posts: 178
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?

Kindly provide your help.

Thanks,
Vijay




Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3817

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".


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
Vijay Chandran
Ranch Hand

Joined: Jan 07, 2007
Posts: 178
Hi,

Thanks for the reply.

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?

Please clarify.

Thanks,
Vijay
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3817

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.


Are you talking about this ?

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


If so, you are adding 12345 to 5432
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Addition problem
 
Similar Threads
valueOf
please explain me the output of the following programs
ValueOf()
i++i+++;
StringBuffer