Usually, when assigning a number to an int larger than 2,147,483,647, then the code won't compile. But in the following snippet, result is larger than (2^31)-1, and it can still print. I know that the "correct" result is obtained by letting result be long , but how come this still prints something and what happens behind the scenes? Thanks in advance.
public class intAssigning
{
static int n = 500000;
static int i;
static int result;
//static long result;
static boolean [] array = new boolean [n];
public static void main(String[] args)
{
for (i = 0; i<n; i++)
{
array[i] = true;
}
for (i=2; i<n; i++)
{
if (array[i])
{
result+=i;
}
}
System.out.println(result);
//result with long: 124999750000
//result with int: 445698416
}
}