Java - Short compareTo() 方法



Java Short compareTo() 方法用於按數值比較兩個 Short 物件。

當我們需要比較兩個給定的 Short 物件的值以檢查這兩個給定的值是否相等、小於或大於時,可以使用此方法。

在物件中,Short 類封裝了 short 型別。short 原生資料型別是一個 16 位帶符號的二進位制補碼整數,其最大值為 32,767,最小值為 -32,768(包含)。

語法

以下是Java Short compareTo()方法的語法:

public int compareTo(Short anotherShort) 

引數

  • anotherShort: 要比較的 short 值。

返回值

此方法返回以下值:

  • 0 如果 anotherShort 在數值上等於此 short。

  • 小於 0 的值,如果此 short 在數值上小於 anotherShort。

  • 大於 0 的值,如果此 short 在數值上大於 anotherShort。

比較包含正 Short 值的兩個 Short 包裝物件示例

當我們將兩個 short 值傳遞給此方法時,如果第一個值小於第二個值,則返回小於 0 的值。

以下示例顯示了 Java Short compareTo() 方法的使用。在這裡,建立了兩個 Short 物件“obj1”和“obj2”。將值分配給這兩個給定的物件,使得第一個值小於第二個值。然後在比較這兩個值後檢索結果:

package com.tutorialspoint;

public class ShortDemo {
   public static void main(String[] args) {
      
      // compares two Short objects numerically
      Short obj1 = new Short("987");
      Short obj2 = new Short("7676");
      int retval = obj1.compareTo(obj2);
      if (retval > 0) {
         System.out.println("obj1 is greater than obj2");
      } else if (retval < 0) {
         System.out.println("obj1 is less than obj2");
      } else {
         System.out.println("obj1 is equal to obj2");
      }
   }
}

輸出

讓我們編譯並執行以上程式,這將產生以下結果:

obj1 is less than obj2

比較包含正 Short 值的兩個 Short 包裝物件示例

當我們將多個整數作為引數傳遞給此方法時,如果第一個值小於第二個值,則返回小於 0 的值;如果第一個值大於第二個值,則返回大於 0 的值;如果兩個值相等,則返回 0。

在以下示例中,建立了三個名為 Shortval1、Shortval2 和 Shortval3 的 Short 物件。然後將這些物件分別賦值為 '50'、'200'、'50' short 變數。然後在物件上呼叫 compareTo() 方法以在不同的給定情況下比較所有物件:

package com.tutorialspoint;

public class ShortDemo {
   public static void main(String[] args) {
      
      // create short object and assign value to it
      short val1 = 50, val2 = 200, val3 = 50;
      Short Shortval1 = new Short(val1);
      Short Shortval2 = new Short(val2);
      Short Shortval3 = new Short(val3);
      
      // returns less than 0 if this Short is less than the argument Short
      int cmp = Shortval1.compareTo(Shortval2); 
      System.out.println("" + Shortval1 + " is less than " + Shortval2 + ",difference = " + cmp);
      
      // returns 0 if this Short is equal to the argument Short
      cmp = Shortval1.compareTo(Shortval3); 
      System.out.println("" + Shortval1 + " is equal to " + Shortval3 + ",difference = " + cmp);
      
      // returns greater than if this Short is greater than the argument Short
      cmp = Shortval2.compareTo(Shortval1); 
      System.out.println("" + Shortval2 + " is more than " + Shortval1 + ",difference = " + cmp);
   }
}   

輸出

以下是上述程式碼的輸出:

50 is less than 200, difference = -150
50 is equal to 50, difference = 0
200 is more than 50, difference = 150

比較包含負 Short 值的兩個 Short 包裝物件示例

當我們使用此方法建立一個 Short 物件陣列時,分別與陣列中的第一個元素進行比較,將返回大於 0、小於 0 和等於 0 的值。

在此示例中,建立了一個 short 型別的陣列,並且透過分配一個 for 迴圈,陣列中的每個物件都與陣列的第一個物件進行比較:

package com.tutorialspoint;

public class ShortDemo {
   public static void main(String[] args) {
      Short[] object = { -854, -546, -854, -917 };
      for (int i = 0; i < 4; i++) {
         
         // comparing every object in the array with the first object            
	      System.out.println(object[0].compareTo(object[i]));
      }
   }
}

輸出

在執行上述程式後,獲得的輸出如下:

0
-308
0
63
java_lang_short.htm
廣告