JAVA integer division

April 25, 08 by the programmer

I was trying some divisions in Java when suddenly I noticed that the result is not the one that I expected

So I took my calculator to check and I noticed that I am not seeing things

So this is what I did

I took 2 numbers and tried to divide them to check the results

Here is what the results were

int i = 7;

int j = 2;

int k = i / j; // gives 3

double res1 = (double) i / ( double) j; // gives 3.5

double res2 = (double) ( i / j ); // gives 3.0

double res3 = ((double) i ) / j; // gives 3.5, j is promoted to double automatically.



So if you take 2 integer numbers and divide them then the result will be an integer number

So be carefull what you divide in JAVA :)

This entry no have comments... but you can be first.

Leave a Reply