public class IntRangeExample {
public static void main(String[] args) {
// Java中int类型的取值范围是-2^31到2^31-1,即-2147483648到2147483647
int minInt = Integer.MIN_VALUE; // -2147483648
int maxInt = Integer.MAX_VALUE; // 2147483647
System.out.println("int类型的最小值: " + minInt);
System.out.println("int类型的最大值: " + maxInt);
// 示例:尝试超出int类型的范围
try {
int overflow = maxInt + 1;
System.out.println("超出最大值后的结果: " + overflow); // 会溢出为最小值
} catch (Exception e) {
System.out.println("捕获到异常: " + e.getMessage());
}
}
}
Integer.MIN_VALUE
和 Integer.MAX_VALUE
是 Java 中的常量,分别表示 int
类型的最小值和最大值。int
类型在 Java 中占用 4 个字节(32 位),因此其取值范围是 -2,147,483,648 到 2,147,483,647。int
类型的值超出其取值范围时,会发生溢出。例如,当 int
类型的最大值加 1 时,它会溢出并变为最小值。上一篇:java byte 转16进制
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站