compareTo() 方法用于将 Number 对象与方法的参数进行比较。可用于比较 Byte, Long, Integer等。spa
该方法用于两个相同数据类型的比较,两个不一样类型的数据不能用此方法来比较。code
语法对象
public int compareTo( NumberSubClass referenceName )
referenceName -- 能够是一个 Byte, Double, Integer, Float, Long 或 Short 类型的参数。blog
若是指定的数与参数相等返回0。it
若是指定的数小于参数返回 -1。编译
若是指定的数大于参数返回 1。class
public class Test{ public static void main(String args[]){ Integer x = 5; System.out.println(x.compareTo(3)); System.out.println(x.compareTo(5)); System.out.println(x.compareTo(8)); } }
编译以上程序,输出结果为:数据类型
1 0 -1