My question appears very simple.
int i =99999;
long square = i*i;
System.out.println(square);   //prints 1409865409 - incorrect value
int i = 99999;
long square = (long) i*i;
System.out.println(square);  // prints 9999800001 - correct value
It looks to be the range issue of int. But shouldn't it typecast the product to long implicitly? What am I missing here? I know how to use Math.pow(double,double) function and it works perfectly. But I want to know the reason for above output.
TIA.
                        
In the first case, the result is first computed as an
int, and only then converted to along.Therefore the wrong result.
In the second case,
iis converted to alongbefore computing the result because(long)(cast tolong) has a higher precedence than*.Therefore the right result.