首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

Byte部类的compare方法

2012-08-24 
Byte类型的compare方法String和Integer类型在使用compareTo方法的时候都会返回0、1或者-1,但是通过阅读java

Byte类型的compare方法
String和Integer类型在使用compareTo方法的时候都会返回0、1或者-1,但是通过阅读java源代码发现如果对Byte类型使用compareTo方法,它返回的是a-b的值。
举例来说:
public class Test{
    public static void main(String[] args) throws IOException {
        Byte b = 10;
        Byte c = -1;
        System.out.println(b.compareTo(c));
    }
}
打印结果是-11
源代码:
public int compareTo(Byte anotherByte) {
return this.value - anotherByte.value;
    }
不明白java为什么要这样设计Byte类型

热点排行