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.