This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Does parenthesis  makes difference on calculation? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Does parenthesis  makes difference on calculation?" Watch "Does parenthesis  makes difference on calculation?" New topic
Author

Does parenthesis makes difference on calculation?

Caglar Cataloglu
Greenhorn

Joined: Feb 06, 2010
Posts: 25
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?


Java Lover
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18509
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18509
    
  40


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
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Caglar Cataloglu
Greenhorn

Joined: Feb 06, 2010
Posts: 25
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?
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

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.


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: Does parenthesis makes difference on calculation?
 
Similar Threads
anonymous inner class and interface
Test the pseudo random generator
if statement help
equals() question required
passing reference problem.......I think :confused: