全屏
java.lang.Byte.compareTo(Byte anotherByte)比较两个字节的对象上的数字。
声明
以下是java.lang.Byte.compareTo()方法的声明public int compareTo(Byte anotherByte)
Specified by
compareTo 在接口 Comparable
参数anotherByte - 该字节进行比较
返回值
此方法返回值0,如果这个字节等于参数字节;值小于0,如果这个字节数值大于参数字节以内;和一个大于0的值,如果这个字节数值大于参数字节(符号比较)。
异常NA
例子
下面的例子显示lang.Byte.compareTo()方法的使用。package cn.sxt;
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
System.out.println( str3 );
}
}
}
让我们来编译和运行上面的程序,这将产生以下结果:Second value is greater
分享到:
0评论
