Java中int的取值范围

Java中int的取值范围是-2^32~2^32-1

先上代码,通过下面代码的执行结果观察

[java]  view plain  copy
  1. public class Test {  
  2.   
  3.     public static void main(String[] args) {  
  4.         // 2的31次方  
  5.         int j = (int) Math.pow(2,31);  
  6.         System.out.println("j的值" + j);  
  7.     }  
  8. }  

运行结果:

j的值2147483647


继续修改代码

[java]  view plain  copy
  1. public class Test {  
  2.   
  3.     public static void main(String[] args) {  
  4.         // 2的31次方  
  5.         int j = (int) Math.pow(2,32);  
  6.         System.out.println("j的值" + j);  
  7.     }  
  8. }  

运行结果(同上面代码):

j的值2147483647


再次修改代码

[java]  view plain  copy
  1. public class Test {  
  2.   
  3.     public static void main(String[] args) {  
  4.         int j = 2147483647;  
  5.         System.out.println("j+1的值" + (j + 1));  
  6.     }  
  7. }  
运行结果:

j+1的值: -2147483648

这里是负值,int的最小值

即当int的最大值+1后,会变成int的最小值

故java中int的取值范围是-2^31~2^31-1,即-2147483648~2147483647。



猜你喜欢

转载自blog.csdn.net/johnt25/article/details/80330520