Java compareTo() 方法

referenceName -- 可以是一个 Byte, Double, Integer, Float, Long 或 Short 类型的参数。

返回值

  • 如果指定的数与参数相等返回0。

  • 如果指定的数小于参数返回 -1。

  • 如果指定的数大于参数返回 1。

实例

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
posted @ 2019-01-16 13:30  lyon♪♫  阅读(251)  评论(0编辑  收藏  举报