Java - Byte compareTo() 方法



描述

Java Byte compareTo(Byte anotherByte) 方法按數值比較兩個 Byte 物件。

宣告

以下是 java.lang.Byte.compareTo() 方法的宣告

public int compareTo(Byte anotherByte)

指定於

介面 Comparable<Byte> 中的 compareTo

引數

anotherByte - 要比較的 Byte

返回值

如果此 Byte 等於引數 Byte,則此方法返回 0;如果此 Byte 在數值上小於引數 Byte,則返回小於 0 的值;如果此 Byte 在數值上大於引數 Byte,則返回大於 0 的值(帶符號比較)。

異常

使用 new 運算子建立的兩個 Byte 物件的比較示例

以下示例演示了使用 new 運算子建立的 Byte 物件的 Byte compareTo() 方法的使用。建立兩個 Byte 變數併為其分配使用 new 運算子建立的 Byte 物件。然後使用 compareTo() 方法比較這兩個變數,並根據 compareTo() 方法的響應列印結果。

package com.tutorialspoint;

public class ByteDemo {
   public static void main(String[] args) {

      // create 2 Byte objects b1, b2
      Byte b1, b2;

      // assign values to b1, b2
      b1 = new Byte("-100");
      b2 = new Byte("10");

      // create an int res
      int res;

      // compare b1 with b2 and assign result to res
      res = b1.compareTo(b2);

      String str1 = "Both values are equal ";
      String str2 = "First value is greater";
      String str3 = "Second value is greater";

      if( res == 0 ) {
      	System.out.println( str1 );
      } else if( res > 0 ) {
      	System.out.println( str2 );
      } else if( res < 0 ) {
      	System.out.println( str3 );
      }
   }
}

輸出

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

Second value is greater

使用 valueOf(String) 方法建立的兩個 Byte 物件的比較示例

以下示例演示了使用 valueOf(String) 方法建立的 Byte 物件的 Byte compareTo() 方法的使用。建立兩個 Byte 變數併為其分配使用 valueOf(String) 方法建立的 Byte 物件。然後使用 compareTo() 方法比較這兩個變數,並根據 compareTo() 方法的響應列印結果。

package com.tutorialspoint;

public class ByteDemo {
   public static void main(String[] args) {

      // create 2 Byte objects b1, b2
      Byte b1, b2;

      // assign values to b1, b2
      b1 = Byte.valueOf("-100");
      b2 = Byte.valueOf("10");

      // create an int res
      int res;

      // compare b1 with b2 and assign result to res
      res = b1.compareTo(b2);

      String str1 = "Both values are equal ";
      String str2 = "First value is greater";
      String str3 = "Second value is greater";

      if( res == 0 ) {
      	System.out.println( str1 );
      } else if( res > 0 ) {
      	System.out.println( str2 );
      } else if( res < 0 ) {
      	System.out.println( str3 );
      }
   }
}

輸出

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

Second value is greater

使用 valueOf(byte) 方法建立的兩個 Byte 物件的比較示例

以下示例演示了使用 valueOf(byte) 方法建立的 Byte 物件的 Byte compareTo() 方法的使用。建立兩個 Byte 變數併為其分配使用 valueOf(byte) 方法建立的 Byte 物件。然後使用 compareTo() 方法比較這兩個變數,並根據 compareTo() 方法的響應列印結果。

package com.tutorialspoint;

public class ByteDemo {
   public static void main(String[] args) {

      // create 2 Byte objects b1, b2
      Byte b1, b2;

      // assign values to b1, b2
      b1 = Byte.valueOf((byte) -100);
      b2 = Byte.valueOf((byte) 10);

      // create an int res
      int res;

      // compare b1 with b2 and assign result to res
      res = b1.compareTo(b2);

      String str1 = "Both values are equal ";
      String str2 = "First value is greater";
      String str3 = "Second value is greater";

      if( res == 0 ) {
      	System.out.println( str1 );
      } else if( res > 0 ) {
      	System.out.println( str2 );
      } else if( res < 0 ) {
      	System.out.println( str3 );
      }
   }
}

輸出

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

Second value is greater
java_lang_byte.htm
廣告