qdu-lkc

compareTo() 方法用于将 Number 对象与方法的参数进行比较。可用于比较 Byte, Long, Integer等。

该方法用于两个相同数据类型的比较,两个不同类型的数据不能用此方法来比较。

 语法

public int compareTo( NumberSubClass referenceName )

参数

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

分类:

技术点:

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-09-19
  • 2022-12-23
  • 2022-12-23
  • 2021-11-23
猜你喜欢
  • 2021-09-19
  • 2022-12-23
  • 2022-12-23
  • 2017-11-23
  • 2021-10-15
  • 2021-09-19
  • 2021-04-18
相关资源
相似解决方案