title: The range of int in Java
body: <p>I understand that the <em>int</em> range in Java should be -2^31 to 2^31-1.</p>
  <p>But when I run this code snippet with 20:</p>
  <pre><code>public class Factorial {
      public int factorial(int n) {
          int fac=1;
          for (int i=1; i&lt;=n; i++) {
              fac *= i;
              System.out.println(&quot;Factorial of &quot; + i + &quot; is: &quot; + fac);
          }
          return fac;
      }
  }
  </code></pre>
  <p>The output:</p>
  <pre><code>Factorial of 1 is: 1
  Factorial of 2 is: 2
  Factorial of 3 is: 6
  Factorial of 4 is: 24
  Factorial of 5 is: 120
  Factorial of 6 is: 720
  Factorial of 7 is: 5040
  Factorial of 8 is: 40320
  Factorial of 9 is: 362880
  Factorial of 10 is: 3628800
  Factorial of 11 is: 39916800
  Factorial of 12 is: 479001600
  Factorial of 13 is: 1932053504
  Factorial of 14 is: 1278945280
  Factorial of 15 is: 2004310016
  Factorial of 16 is: 2004189184
  Factorial of 17 is: -288522240
  Factorial of 18 is: -898433024
  Factorial of 19 is: 109641728
  Factorial of 20 is: -2102132736
  </code></pre>
  <p>It's not making sense from 13 (13! = 6,227,020,800). It looks like it's out of range and wrapped around. What's wrong? Is it due to <a href="https://en.wikipedia.org/wiki/Eclipse_%28software%29" rel="nofollow noreferrer">Eclipse</a> that I'm using?</p>
  <p>Though I think it's not relevant, here is the test code:</p>
  <pre><code>public class TestFac {
  
      public static void main(String[] args) {
          int n;
          Scanner sc = new Scanner(System.in);
  
          System.out.println(&quot;Input num you want to factorial: &quot;);
          n = sc.nextInt();
          Factorial fac = new Factorial();
          fac.factorial(n);
      }
  }
  </code></pre>