"equals"和"=="的区别

2018-07-09  本文已影响0人  超人有点忙

最近公司在招新人,作为一个也仅仅工作不到一年的小白被老板安排打电话初步筛选刚毕业的初级java开发人员。对于我来说当然是一个挑战,也是一个锻炼自己的机会,为了不给公司和自己丢脸,认真的准备面试问题,其中我把这个问题放在了第一个,既是我长时间来没有彻彻底底搞明白的问题,也是一个自认为初学者刚开始容易弄混淆的问题。

堆、栈

首先在了解它们区别之前,应该多少的了解一些堆栈的概念,对深刻理解有好处。
首先我们编写的程序都是JVM帮我们编译运行的,运行的时候他会在内存模型中分配两个内存,一个堆内存,一个栈内存

区别

有了一些堆栈的概念我们在来谈区别。

    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;
    }

自己和自己 做== 肯定返回 true,然后将string 都转换成 char 类型的数组,再通过while循环,来计较每一位的char是否完全相同。如果没有重写过equals方法,那么它的作用跟"=="是完全一样的

举例

public class TestEquals {
    class Dog {
        String name;
        String color;
        public Dog () {

        };
        public Dog (String name, String color) {
            this.name = name;
            this.color = color;
        }

         @Override                                                               
         public boolean equals(Object obj) {                                     
             if (this == obj) {                                                  
                 return true;                                                    
             }                                                                   
             if (obj instanceof Dog) {                                           
                 Dog d = (Dog) obj;                                              
                 return this.name.equals(d.name) && this.color.equals(d.color);  
             } else {                                                            
                 return false;                                                   
             }                                                                   
         }                                                                       

    }

    public static void main (String args[]) {
        TestEquals testEquals = new TestEquals();
        Dog dog1 = testEquals.new Dog("Tom","black");
        Dog dog2 = testEquals.new Dog("Tom","black");
        String a = new String("abc");
        String b = new String ("abc");
        String aa = "abc";
        String bb = "abc";
        Long l1 = 127L;
        Long l2 = 127L;
        Long l3 = 128L;
        Long l4 = 128L;
        System.out.println("dog1.equals(dog2): " + dog1.equals(dog2));
        System.out.println("a.equals(b): " + a.equals(b));
        System.out.println("a == b: " + (a==b));
        System.out.println("aa == bb: " + (aa==bb));
        System.out.println("l1 == l2: " + (l1 == l2));
        System.out.println("l3 == l4: " + (l3 == l4));
    }

dog1.equals(dog2): true
a.equals(b): true
a == b: false
aa == bb: true
l1 == l2: true
l3 == l4: false
上一篇 下一篇

猜你喜欢

热点阅读