Java.lang.Byte.compareTo() 方法
描述
java.lang.Byte.compareTo(Byte anotherByte) 比较两个 Byte 对象的数值。
声明
以下是 java.lang.Byte.compareTo() 方法的声明。
public int compareTo(Byte anotherByte)
指定者
接口 Comparable<Byte> 中的 compareTo
参数
anotherByte − 要比较的字节
返回值
如果此 Byte 等于参数 Byte,则此方法返回值 0; 如果此 Byte 在数值上小于参数 Byte,则值小于 0; 如果此 Byte 在数值上大于参数 Byte(有符号比较),则值大于 0。
异常
NA
示例
下面的例子展示了 lang.Byte.compareTo() 方法的使用。
package com.tutorialspoint;
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

