package com.company; public class Main { public static void main(String[] args) { Integer i = new Integer(1000); Integer j = new Integer(100); compares(i,j); } /** * 使用Integer实例的compareTo方法来比较两个Integer的值, * 如果返回值为0 说明两个数相等 * 如果返回值为1 说明第一个数大于第二个数 * 如果返回值为-1 说明第一个值小于第二个数 * * @param i * @param j */ private static void compares(Integer i, Integer j) { System.out.println(i.compareTo(j)); if(i.compareTo(j) == 0 ){ System.out.println("两个数相等"); }else if(i.compareTo(j) > 0){ System.out.println("第一个数大于第二个数"); }else if(i.compareTo(j) < 0 ){ System.out.println("第一个数小于第二个数"); } } }
java中compareTo的用法
最新推荐文章于 2024-07-08 23:43:50 发布