Java the range of int?

坚强是说给别人听的谎言 提交于 2019-11-28 13:12:27

Here I would like to mention the concept of integer clock. Max and min values for int in java are int MAX_VALUE = 2147483647 int MIN_VALUE = -2147483648

please check the following results

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

Output:

a:2147483645
a:2147483646
a:2147483647
a:-2147483648
a:-2147483647
a:-2147483646
a:-2147483645
a:-2147483644
a:-2147483643
a:-2147483642

It shows that when you go beyond the limit of +ve range of integer, the next values starts from its negative starting value again.

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

If you calculate the factorial of 13 it is 6227020800. This value goes beyond int range of java. So new value will be

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

So, in your answer factorial of 13 is coming 1932053504. This is how integer clock works.

You can use long datatype instead of integer to achieve your purpose. For any queries you can post here.

Boyang

From http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html:

The int data type is a 32-bit signed two's complement integer. It has a minimum value of -2,147,483,648 and a maximum value of 2,147,483,647 (inclusive). For integral values, this data type is generally the default choice unless there is a reason (like the above) to choose something else. This data type will most likely be large enough for the numbers your program will use, but if you need a wider range of values, use long instead.

Moral of the story: Never believe in your teacher blindly!

If you check Java Integer, its maximum and minimum values are as follows:

int MAX_VALUE = 2147483647
int MIN_VALUE = -2147483648

If you do some maths, you will see that (Factorial of 13) 1932053504 * 14 is 27048749056 which is beyond int MAX_VALUE and that's why you are getting wrong result for Factorial of 14. So in order to have good results it is better to use long type instead.

Factorial 13 is 6227020800. This is more than 31 bits long so it's wrapped around.

If you want to support BIG numbers (e.g. arbitrary length) then consider using the BigInteger class which provides an unlimited range.

an0nh4x0r

Please run this code:

System.out.println("Minimum value of Integer is: " + Integer.MIN_VALUE);
System.out.println("Maximum value of Integer is: " + Integer.MAX_VALUE);

So you can see why it fails.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!