【JAVA】基本数据类型和引用类型的比较


1.基本数据类型的比较

(1). byte、short、int、long

这几个类型都属于整形家族,比较的方法简单粗暴,直接用 == 即可

下面是这几个类型比较的例子:

public class Test{
    public static void main(String[] args) {
        byte a = 4;
        byte b = 4;
        System.out.println(a == b);

        short c = 21;
        short d = 20;
        System.out.println(c == d);

        int e = 3;
        int f = 5;
        System.out.println(e == f);

        long g = 17;
        long h = 17;
        System.out.println(g == h);
    }
}

运行结果:

true
false
false
true

(2). boolean

布尔类型也直接用双等号

public class Test{
    public static void main(String[] args) {
        //变量之间的比较
        boolean a = true;
        boolean b = false;
        System.out.println(a == b);

        //直接比较
        if (a == true) {
            System.out.println("a为true");
        }

    }
}

运行结果:

true
a为true

(3). float、double

由于浮点型数据在内存里的存储比较特殊,在比较是并不能直接用 ==

这里介绍两种可行的方法

I.范围误差比较法

public class Test{
    public static void main(String[] args) {
        float f1 = (float)1.00000001;
        float f2 = (float)1.00000002;
        if (Math.abs(f1 - f2) < 0.00001) {
            System.out.println("f1 == f2");
        } else {
            System.out.println("f1 != f2");
        }
        
        double d1 = 2.22222222;
        double d2 = 2.22222242;
        if (Math.abs(d1 - d2) < 0.000000000000001) {
            System.out.println("d1 == d2");
        } else {
            System.out.println("d1 != d2");
        }
    }
}

输出:

f1 == f2
d1 != d2

说明:
这里姑且认为
f1 和 f2 相差在0.00001以内就相等
d1 和 d2相差在0.000000000000001就相等
实际使用时还得看需求

II.使用BigDecimal

public class Test{
   public static void main(String[] args) {
        BigDecimal decimal1 = new BigDecimal("0.1");
        BigDecimal val1 = new BigDecimal("0.1");
        for (int i = 0; i < 10; i++) {
            decimal1.add(val1);
        }
        
        BigDecimal decimal2 = new BigDecimal("0.1");
        BigDecimal val2 = new BigDecimal("10");
        decimal2.multiply(val2);
        
		//用BigDecimal里面的compareTo方法比较两个对象
        int ret = decimal1.compareTo(decimal2);
        if (ret == 0) {
            System.out.println("decimal1's value equals decimal2's value");
        } else {
            System.out.println("decimal1's value not equals decimal2's value");
        }
}

输出:

decimal1's value equals decimal2's value

2.引用类型的比较

整形,布尔类型,浮点型都是比较简单的类型
如果遇到复杂类型,我们就必须指定比较规则

比如汽车:
我们可以用车的出厂日期进行比较
也可以用车的售价进行比较
还可以先用出厂日期先进行比较,如果日期一样,再用车的售价进行比较


下面介绍三种比较方法:

(1). 重写equals方法

学生类

public class Student {
    private String name;
    private int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public boolean equals(Object o) {
        Student other = (Student)o;
        if (this.age == other.age && this.name.equals(other.name)) {
            return true;
        }
        return false;
    }

}

说明:
这里规定的比较规则是: 年龄一样和名字一样就返回true, 否则返回false


测试类

public class Test{
    public static void main(String[] args) {
        Student s1 = new Student("jack", 22);
        Student s2 = new Student("john", 22);
        
		System.out.println(s1.equals(s2));
    }
}

输出:

false

(2). 实现Comparable接口

同样还是学生类,我们实现一下Comparable接口

public class Student implements Comparable<Student>{
    private String name;
    private int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public int compareTo(Student o) {
        if (this.age > o.age) {
            return 1;
        } else if (this.age < o.age) {
            return -1;
        } else {
            return 0;
        }
    }
}

测试类

public class Test{
    public static void main(String[] args) {
        Student s1 = new Student("小明", 25);
        Student s2 = new Student("小红", 22);
        System.out.println(s1.compareTo(s2));
    }
}

输出

1

说明:
比较的规则在comparaTo方法中指定
这里只指定了年龄,大于则返回1,小于返回-1,等于返回0


(3). 比较器Comparator

比较器跟前两种重写方法有区别,比较器需要单独写一个类
这里同样也以 年龄(age) 作为比较基准

学生类

public class Student {
    private String name;
    private int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

比较器

public class StudentComparator implements Comparator<Student> {
    @Override
    public int compare(Student o1, Student o2) {
        if (o1.getAge() > o2.getAge()) {
            return 1;
        } else if (o1.getAge() < o2.getAge()) {
            return -1;
        } else {
            return 0;
        }
    }
}

测试类

public class Test{
    public static void main(String[] args) {
        Student s1 = new Student("李明", 25);
        Student s2 = new Student("王红", 22);
        StudentComparator studentComparator = new StudentComparator();
        int ret = studentComparator.compare(s1, s2);
        System.out.println(ret);
    }
}

输出

1

解释
1.这里的比较,其实是交给了比较器来实现,我们不需要在学生类里面写比较方法
2.要使一个类变成比较器,必须实现Comparator接口,指定要比较的类型,重写compara方法
3.比较规则在compara方法里指定,根据自己的需要指定规则
4.之后创建比较器对象,就可以调用compara方法,来比较两个对象


点个赞再走吧💛💛💛
请添加图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值