谈谈java的"=="和equals

来源:互联网 发布:java内存限制 编辑:程序博客网 时间:2024/06/01 10:10

一、==和equals的使用

在工作中,肯定会遇到比较两者之间相等的情况,对于初学者,可能在使用过程中会碰到一些问题而产生疑问,这里对 “==”运算符和equals方法的使用做一个简单的介绍。

“==”的使用

1、如果两者是基本数据类型,则比较的是两者的值是否相等2、如果两者是对象,则比较的是它们的引用的地址是否相等

equals方法的使用

1、如果该方法被重写,则已重写的规则为准2、如果该方法未被重写,则比较两个对象的引用地址是否相等

二、通过问题学习==和equals

在了解了 “==”和equals的使用方法之后,我们通过示例来深入理解二者之间的使用。

2.1 基本数据类型比较

    /**     * 通过例子分析出'=='和equals的使用     */    public static void main(String[] args) {        int m1 = 10, m2 = 10;        double d1 = 10, d2 = 10.00;        System.out.println("m1 == m2 : " + (m1 == m2));        System.out.println("d1 == d2 : " + (d1 == d2));        System.out.println("m1 == d1 : " + (m1 == d1));        System.out.println("d1 == d2 : " + (d1 == d2));    }

执行代码,运行结果如下:

m1 == m2 : trued1 == d2 : truem1 == d1 : trued1 == d2 : true

基本数据类型的比较,就是比较两者的值是否相等。

2.2 基本数据类型的包装类比较

查看以下代码:

    /**     * 通过例子分析出'=='和equals的使用     */    public static void main(String[] args) {        int num1 = 10;        Integer num2 = 10;        Integer num3 = new Integer(10);        Integer num4 = new Integer(num1);        Integer num5 = num4;        System.out.println("num1 == num2 : " + (num1 == num2));        System.out.println("num1 == num3 : " + (num1 == num3));        System.out.println("num1 == num4 : " + (num1 == num4));        System.out.println("num2 == num3 : " + (num2 == num3));        System.out.println("num3 == num4 : " + (num3 == num4));        System.out.println("num4 == num5 : " + (num4 == num5));        System.out.println("num2.equals(num1) : " + num2.equals(num1));        System.out.println("num2.equals(num3) : " + num2.equals(num3));        System.out.println("num3.equals(num4) : " + num3.equals(num4));        System.out.println("num4.equals(num4) : " + num4.equals(num5));    }

执行代码,运行结果如下:

num1 == num2 : truenum1 == num3 : truenum1 == num4 : truenum2 == num3 : falsenum3 == num4 : falsenum4 == num5 : truenum2.equals(num1) : true num2.equals(num3) : truenum3.equals(num4) : true num4.equals(num4) : true 

为什么基本数据类型与它对应的包装类的比较都相等呢?这是因为j2se 5.0以后提供了自动装箱和自动拆箱的功能。
例如:

    Integer m = 100;  //自动装箱,实际上在执行的时候,系统为我们自动执行了Integer i = Integer.valueOf(100);    int n = m; //自动拆箱,实际上在执行的时候,自动执行了int n = m.intValue();

所以前三个为自动拆箱,与num1比较的是实际的值,所以输出都为true。num2、num3、num4所指的地址都不一样,所以在使用”==”进行比较时,比较的是引用的地址,所以输出都为false。Integer类重写了equals方法:

/**     * Compares this object to the specified object.  The result is     * {@code true} if and only if the argument is not     * {@code null} and is an {@code Integer} object that     * contains the same {@code int} value as this object.     *     * @param   obj   the object to compare with.     * @return  {@code true} if the objects are the same;     *          {@code false} otherwise.     */    public boolean equals(Object obj) {        if (obj instanceof Integer) {            return value == ((Integer)obj).intValue();        }        return false;    }

它实际上比的是它们的值,所以后面都输出为true。

2.3 对象的比较

查看以下代码:

package com.chavin.javabasedemo;/** * 分析'=='和equals *  * @author chavin *  */public class Demo {    /**     * 通过例子分析出'=='和equals的使用     */    public static void main(String[] args) {        String str = "hello!";        String str1 = new String("hello!");        String str2 = new String("hello!");        String str3 = str2;        System.out.println("str == str1 : " + (str == str1));        System.out.println("str1 == str2 : " + (str1 == str2));        System.out.println("str2 == str3 : " + (str2 == str3));        System.out.println("str.equals(str1) : " + str.equals(str1));        System.out.println("str1.equals(str2) : " + str1.equals(str2));        System.out.println("str2.equals(str3) : " + str2.equals(str3));        Person chavin1 = new Person("chavin", 22);        Person chavin2 = new Person("chavin", 22);        Person chavin3 = chavin2;        System.out.println("chavin1 == chavin2 : " + (chavin1 == chavin2));        System.out.println("chavin1.equals(chavin2) : " + (chavin1.equals(chavin2)));        System.out.println("chavin2.equals(chavin3) : " + (chavin2.equals(chavin3)));    }}class Person {    private String name;    private int age;    public Person(String name, int age) {        super();        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;    }}

执行代码,运行结果如下:

str == str1 : false    //①str1 == str2 : false   //②str2 == str3 : true    //③str.equals(str1) : true   //④str1.equals(str2) : true  //⑤str2.equals(str3) : true  //⑥chavin1 == chavin2 : false   //⑦chavin1.equals(chavin2) : false  //⑧chavin2.equals(chavin3) : true   //⑨

对于对象,==比较的是引用的地址,所以①、②、⑦不相等,③相等,equals比较对象时也是比较的引用的地址,所以⑨相等。对于String,重写了equals方法:

    /**     * Compares this string to the specified object.  The result is {@code     * true} if and only if the argument is not {@code null} and is a {@code     * String} object that represents the same sequence of characters as this     * object.     *     * @param  anObject     *         The object to compare this {@code String} against     *     * @return  {@code true} if the given object represents a {@code String}     *          equivalent to this string, {@code false} otherwise     *     * @see  #compareTo(String)     * @see  #equalsIgnoreCase(String)     */    public boolean equals(Object anObject) {        if (this == anObject) {            return true;        }        if (anObject instanceof String) {            String anotherString = (String) anObject;            int n = value.length;            if (n == anotherString.value.length) {                char v1[] = value;                char v2[] = anotherString.value;                int i = 0;                while (n-- != 0) {                    if (v1[i] != v2[i])                            return false;                    i++;                }                return true;            }        }        return false;    }

它在重写后,实际上是比较值是否相等,所以④、⑤、⑥相等。

声明:本文是由本人通过学习而写出的,文中不保证所有描述全是正确的,如果错误之处还请指出,一起探讨学习。

0 0
原创粉丝点击