• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Does parenthesis makes difference on calculation?

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My problem is getting different results of a calculation with using parenthesis and without parenthesis.

When i run this line of code, i get the result as above.

Result is = 12345678987654321
But when i run this, i get a weird result.


Result is = 1653732529

I know that if you do not use casting to long type, you will loose some data because the operations on Java is calculated in integers, right?

Any idea what is going on here?
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Caglar Cataloglu wrote:
I know that if you do not use casting to long type, you will loose some data because the operations on Java is calculated in integers, right?

Any idea what is going on here?



Exactly what you stated.

Henry
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

To give more details....

In the first example, the first int was converted to a long; a long is then multiplied with an int, yielding another long; and this long is then assigned.

In the second example, the two ints are multiplied. This multiplication yields an int which isn't big enough to hold the result. This result is then converted to a long and assigned.

Henry
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Caglar Cataloglu wrote:


You're casting only int1 to long, then perform long * int calculation that returns a long.


You're performing int * int calculation that returns an int. You then cast this int to a long.
 
Caglar Cataloglu
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Henry and Rob for clarifying this issue. Now i got the idea.

I have 1 more question.
Is it convenient to make more than 10 digit calculations using long data type via casting integers to long data types?
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, what exactly do you mean by "convenient"? If the numbers you are doing calculations with do not fit in a 32-bit int, then you need to use something else, such as a 64-bit long. It's not really a matter of convenience.
 
reply
    Bookmark Topic Watch Topic
  • New Topic