Your implicitly-typed variables are all being created as int
, but the result of your calculation will only fit in a long
, so you are seeing an overflow error.
If you simply declare result
as a long
instead, you will still see the wrong result, because the operation is performed on two int
s and not converted to long
until the assignment, by
which time the overflow has already occurred.
So instead, you need to either declare one (or both) of your numbers as long
, or cast one (or both!) to long
during the calculation. You can also omit the redundant assigning of 0
to `result:
int firstNumber = 654165;
int secondNumber = 6541;
long result = (long)firstNumber * secondNumber;
or:
long firstNumber = 654165;
int secondNumber = 6541;
long result = firstNumber * secondNumber;