变量的比较,==和equals的区别

来源:互联网 发布:淘宝t恤100字好评评语 编辑:程序博客网 时间:2024/06/04 00:25

变量之间的比较

int、Integer、string的比较
public class Compare {public static void main(String[] args) {System.out.println("Compare of int");int a = 12;int b = 34;int c = 46;int d = 46;boolean result1 = (a==b);boolean result2 = ((a+b)==c);boolean result3 = (c==d);System.out.println("a==b? " + result1 + "\n(a+b)==c? " + result2 + "\nc==d? " + result3 + "\n------------------------------------");System.out.println("Compare of integer");Integer t1 = new Integer(a);Integer t2 = new Integer(b);Integer t3 = new Integer(c);Integer t4 = new Integer(d);boolean r1 = t1.equals(t2);boolean r2 = t3.equals(t1+t2);boolean r3 = t3.equals(t4);System.out.println("t1==t2? " + (t1==t2) + "\n(t1+t2)==t3? " + ((t1+t2)==t3) + "\nt3==t4? " + (t3==t4) + "\n-------------------------");System.out.println("t1.equals(t2)? " + r1 + "\n(t1+t2).equals(t3)? " + r2 + "\nt3.equals(t4)? " + r3 + "\n------------------------------------");System.out.println("Compare of String");String s1 = "123";String s2 = "456";String s3 = "123456";String s4 = "123456";System.out.println("s1==s2? " + (s1==s2) + "\ns3==s1+s2? " + (s3==(s1+s2)) + "\ns3==s4? " + (s3==s4) + "\n---------------------------");System.out.println("s1.equals(s2)? "+ s1.equals(s2) + "\ns3.equals(s1+s2)? " + s3.equals(s1+s2) + "\ns3.equals(s4)? " + s3.equals(s4) + "\n------------------------------------");}}

结果:
Compare of int
a==b? false
(a+b)==c? true
c==d? true
------------------------------------
Compare of integer
t1==t2? false
(t1+t2)==t3? true
t3==t4? false
-------------------------
t1.equals(t2)? false
(t1+t2).equals(t3)? true
t3.equals(t4)? true
------------------------------------
Compare of String
s1==s2? false
s3==s1+s2? false
s3==s4? true
---------------------------
s1.equals(s2)? false
s3.equals(s1+s2)? true
s3.equals(s4)? true
------------------------------------



==和equals的区别与分析


当比较基本数据类型的时候只能用==,来比较内存中存储的数值是否相同。

当==用来比较两个对象类型的时候,涉及对象的内存分为两块,一块是在栈内存的对象引用,一块是堆内存的对象实例。

这时候用==比较就会比较内存中的数值是否相等,这就涉及到对象内存的首地址指向的是否是同一个对象实例。


而equals并不能简单的说是比较什么的

Object中的equals默认方法就是两个对象的==比较。

public boolean equals(Object obj) {return (this == obj);}


String类中重写的方法

private final char value[];

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;    }
先用==比较,再用instanceof判断类型,再转为char数组一个个字符比较。


Integer类中重写的equals

public boolean equals(Object obj) {        if (obj instanceof Integer) {            return value == ((Integer)obj).intValue();        }        return false;    }


所以equals怎么比较并不重要,除了那些没有必要的,equals是用来重写的,来定义自己的两个obj满足什么条件是equals的,

当一个类没有定义equals的时候才会继承Object的equals方法。

如果实在要一个归纳,那么==就是比较是否是同一个东西,而equals是用来比较按既定的逻辑是否是相同的。







原创粉丝点击