Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Addition problem

 
Vijay Chandran
Ranch Hand
Posts: 186
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3913
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3913
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15288
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic