目录

int compareTo(Byte anotherByte)

描述 (Description)

java.lang.Byte.compareTo(Byte anotherByte)数字方式比较两个Byte对象。

声明 (Declaration)

以下是java.lang.Byte.compareTo()方法的声明

public int compareTo(Byte anotherByte)

指定 (Specified by)

compareTo in interface Comparable《Byte》

参数 (Parameters)

anotherByte - 要比较的字节

返回值 (Return Value)

如果此Byte等于参数Byte,则此方法返回值0; 如果此Byte在数值上小于参数Byte,则小于0的值; 如果此字节在数值上大于参数字节(签名比较),则值大于0。

异常 (Exception)

NA

例子 (Example)

以下示例显示了lang.Byte.compareTo()方法的用法。

package com.iowiki;
import java.lang.*;
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
↑回到顶部↑
WIKI教程 @2018