Soldato
Just playing around with casting to see the effect it has if you don't do it properly!
For example 123456789^2 = 15241578750190521
The following code produces the result -1757895751
Where as this produces the correct result
Question is why does the first one produce an incorrect result, I would have thought casting the result of (int1*int2) would produce exactly the same thing as (long)int1*int2??????
For example 123456789^2 = 15241578750190521
The following code produces the result -1757895751
Code:
public class Order {
public int int1 = 123456789;
public int int2 = 123456789;
public long long1 = 0;
public void calculateTotal(){
[B] long long1 = (long)(int1*int2);[/B]
System.out.println("Result: " + long1);
}
}
Where as this produces the correct result
Code:
public class Order {
public int int1 = 123456789;
public int int2 = 123456789;
public long long1 = 0;
public void calculateTotal(){
[B] long long1 = (long)int1*int2;[/B]
System.out.println("Result: " + long1);
}
}
Question is why does the first one produce an incorrect result, I would have thought casting the result of (int1*int2) would produce exactly the same thing as (long)int1*int2??????