Java 中的 Integer.MAX_VALUE 和 Integer.MIN_VALUE 示例


Java 的 Integer 类提供两个名为 Integer.MAX_VALUE 和 Integer.MIN_VALUE 的常量,它们分别表示 Java 中整型变量的最大值和最小值。Integer.MAX_VALUE 的实际值是 231 - 1,等于 2147483647;Integer.MIN_VALUE 的实际值是 -231,等于 -2147483648。为了方便,我们在程序中使用它们的常量表示,而不是编写这些大的整数。本文旨在探讨 Integer.MAX_VALUE 和 Integer.MIN_VALUE 的用途以及如何在 Java 程序中使用它们。

Java 中 Integer.MAX_VALUE 和 Integer.MIN_VALUE 的示例

在本节中,我们将了解在 Java 程序中使用 Integer.MAX_VALUE 和 Integer.MIN_VALUE 的不同方法。

示例 1

下面的示例展示了如何使用 Integer.MIN_VALUE 和 Integer.MAX_VALUE 来显示 Java 中整型变量的最大值和最小值。

public class Example1 {
   public static void main(String[] args) {
      // storing the minimum and maximum values
      int smallest = Integer.MIN_VALUE;
      int largest = Integer.MAX_VALUE;
      // to show the smallest and largest integer values
      System.out.println("The smallest value is: " + smallest);
      System.out.println("The largest value is: " + largest);
   }
}

输出

The smallest value is: -2147483648
The largest value is: 2147483647

示例 2

下面的示例演示了如何使用 Integer.MIN_VALUE 和 Integer.MAX_VALUE 从数组中查找最大和最小元素。

方法

  • 第一步是声明并初始化一个包含 5 个整型元素的数组。

  • 现在,将最小值和最大值存储到整型变量中。

  • 定义一个 for-each 循环来遍历已定义的数组。

  • 在这个循环中,使用两个 if 块来比较并更新给定数组中的最小和最大元素。

  • 最后,打印结果并退出。

public class Example2 {
   public static void main(String[] args) {
      // initializing an array of integers
      int[] numericList = {10, -5, 3, 7, 15};
      // storing the minimum and maximum values
      int smlst = Integer.MAX_VALUE;
      int lrgst = Integer.MIN_VALUE;
      // to loop through the given array
      for (int number : numericList) {
         // to compare each element with smallest and largest
         if (number < smlst) {
            // to update smallest if a smaller element is found
            smlst = number;
         }
         if (number > lrgst) {
            // to update largest if a larger element is found
            lrgst = number;
         }
      }
      // to show the smallest and largest element from array
      System.out.println("The smallest element is: " + smlst);
      System.out.println("The largest element is: " + lrgst);
   }
}

输出

The smallest element is: -5
The largest element is: 15

Integer.MAX_VALUE 和 Integer.MIN_VALUE 的溢出条件

当操作结果超过数据类型的范围时,就会发生溢出条件。如果尝试将 1 加到 Integer.MAX_VALUE,则结果将为 Integer.MIN_VALUE。类似地,如果尝试从 Integer.MIN_VALUE 中减去 1,则结果将为 Integer.MAX_VALUE。

示例 3

在这个示例中,我们将展示 Integer.MAX_VALUE 和 Integer.MIN_VALUE 常量的溢出条件的实际实现。

public class Example3 {
   public static void main(String[] args) {
      // storing the minimum and maximum values
      int smallest = Integer.MIN_VALUE;
      int largest = Integer.MAX_VALUE;
      // to show the smallest and largest integer values
      System.out.println("The value after subtracting 1 from Integer.MIN_VALUE is: " + (smallest - 1));
      System.out.println("The value after adding 1 to Integer.MAX_VALUE is: " + (largest + 1));
   }
}

输出

The value after subtracting 1 from Integer.MIN_VALUE is: 2147483647
The value after adding 1 to Integer.MAX_VALUE is: -2147483648

结论

这两个常量 Integer.MAX_VALUE 和 Integer.MIN_VALUE 在执行各种操作(例如从整数集合中查找最大和最小元素)时非常方便。在本文中,我们通过示例程序学习了使用这两个常量的方法。

更新于:2023年7月20日

6K+ 次查看

启动你的职业生涯

完成课程获得认证

开始学习
广告
© . All rights reserved.