Java的范围是什么?

Boy*_*ang 2 java int range

我知道Java中的int范围应该是-2 ^ 31到2 ^ 31-1.但是当我用20运行这段代码片段时:

public class Factorial {
    public int factorial(int n) {
        int fac=1;
        for (int i=1; i<=n; i++) {
            fac *= i;
            System.out.println("Factorial of " + i + " is: " + fac);
        }

        return fac;
    }
}
Run Code Online (Sandbox Code Playgroud)

输出:

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
Run Code Online (Sandbox Code Playgroud)

它从13开始没有意义.看起来它超出范围并且四处乱窜.怎么了?是因为我正在使用Eclipse吗?

虽然我认为它不相关,但这里是测试代码:

public class TestFac {

    public static void main(String[] args) {
        int n;
        Scanner sc = new Scanner(System.in);

        System.out.println("Input num you want to factorial: ");
        n = sc.nextInt();
        Factorial fac = new Factorial();
        fac.factorial(n);
    }

}
Run Code Online (Sandbox Code Playgroud)

Jay*_*Jay 20

在这里我想提一下整数时钟的概念.java中int的最大值和最小值为int MAX_VALUE = 2147483647 int MIN_VALUE = -2147483648

请检查以下结果

 int a = 2147483645;
 for(int i=0;i<10;i++) {
    System.out.println("a:"+ a++);
 }
Run Code Online (Sandbox Code Playgroud)

输出:

a:2147483645
a:2147483646
a:2147483647
a:-2147483648
a:-2147483647
a:-2147483646
a:-2147483645
a:-2147483644
a:-2147483643
a:-2147483642
Run Code Online (Sandbox Code Playgroud)

它表明当你超出+ ve整数范围的限制时,下一个值再次从其负起始值开始.

 -2147483648,       <-----------------
 -2147483647,                        | 
 -2147483646,                        |  
  .                                  |
  .                                  |
  .                                  |    (next value will go back in -ve range)
  0,                                 |
 +1,                                 |
 +2,                                 |   
 +3,                                 |
  .                                  |
  .                                  |
  .,                                 |
 +2147483645,                        |    
 +2147483646,                        | 
 +2147483647     ---------------------
Run Code Online (Sandbox Code Playgroud)

如果计算阶乘13,则为6227020800.此值超出java的int范围.所以新的价值将是

        6227020800
      - 2147483647 (+ve max value)
   -----------------
Value = 4079537153
      - 2147483648 (-ve max value)  
   -----------------
value = 1932053505
   -             1  (for zero in between -ve to +ve value)
  ----------------
Answer = 1932053504
Run Code Online (Sandbox Code Playgroud)

所以,在你的答案13中的阶乘即将到来1932053504.这就是整数时钟的工作原理.

您可以使用long数据类型而不是整数来实现您的目的.您可以在此处发布任何疑问.


Boy*_*ang 3

原始数据类型

数据int类型是 32 位有符号二进制补码整数。最小值为 -2,147,483,648,最大值为 2,147,483,647(含)。对于整数值,此数据类型通常是默认选择,除非有理由(如上所述)选择其他类型。对于您的程序将使用的数字来说,此数据类型很可能足够大,但如果您需要更广泛的值,请改用 long。

这个故事的寓意是:永远不要盲目相信你的老师!

  • 这个故事的实际寓意是:*仔细*听老师的话。他所说的,*可能是正确的*。老师给你的哪些信息是错误的?从您发布的内容来看,只有您的解释似乎是错误的。 (7认同)