美文网首页
equals和==的区别,字符串相等判定,Arraylist内容

equals和==的区别,字符串相等判定,Arraylist内容

作者: Visualing | 来源:发表于2018-03-26 21:25 被阅读0次

equals和==的区别

==:

== 比较的是变量(栈)内存中存放的对象的(堆)内存地址,用来判断两个对象的地址是否相同,即是否是指相同一个对象。比较的是真正意义上的指针操作。

1、比较的是操作符两端的操作数是否是同一个对象。
2、两边的操作数必须是同一类型的(可以是父子类之间)才能编译通过。
3、比较的是地址,如果是具体的阿拉伯数字的比较,值相等则为true

public static void main(String[] args) {
      int a=10;
      long b=10L;
      double c=10.0;
      //int a=10 与 long b=10L 与 double c=10.0都是相同的(为true),因为他们都指向地址为10的堆。
      System.out.println(a == b);//true
      System.out.println(b == c);//true
}

equals:

equals用来比较的是两个对象的内容是否相等,由于所有的类都是继承自java.lang.Object类的,所以适用于所有对象,如果没有对该方法进行覆盖的话,调用的仍然是Object类中的方法,而Object中的equals方法返回的却是==的判断。否则就是通过equals逻辑判定是否相等

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

字符串相等判定

String s="abce"是一种非常特殊的形式,和new 有本质的区别。它是java中唯一不需要new 就可以产生对象的途径。
以String s="abce"形式赋值在java中叫直接量,它是在常量池中而不是象new一样放在压缩堆中。

这种形式的字符串,在JVM内部发生字符串拘留,即当声明这样的一个字符串后,JVM会在常量池中先查找有有没有一个值为"abcd"的对象,如果有,就会把它赋给当前引用.即原来那个引用和现在这个引用指点向了同一对象,如果没有,则在常量池中新创建一个"abcd",下一次如果有String s1 = "abcd";又会将s1指向"abcd"这个对象,即以这形式声明的字符串,只要值相等,任何多个引用都指向同一对象.
而String s = new String("abcd");和其它任何对象一样.每调用一次就产生一个对象,只要它们调用。

也可以这么理解:
String str = "hello"; 先在内存中找是不是有"hello"这个对象,
如果有,就让str指向那个"hello".
如果内存里没有"hello",就创建一个新的对象保存"hello".
String str=new String ("hello") 就是不管内存里是不是已经有"hello"这个对象,都新建一个对象保存"hello"。

字符串相等

public class TestMain {
        public static void main(String[] args) {
            String s1 = "windows";
            String s2 = "win"+ new String("dows");
            System.out.println(s1 == s2);
        }
}
false

Process finished with exit code 0

演示示例

public class TestMain {
        public static void main(String[] args) {
            String s1 = "windows";
            String s2 = "win"+ new String("dows");

            String s3 = "windows"; 
            String s4 = new String("windows"); 

            System.out.println(s1 == s2);//false
            System.out.println(s1 == s3);//true
            System.out.println(s1 == s4);//false

            System.out.println(s1.equals(s2));//true
            System.out.println(s1.equals(s3));//true
            System.out.println(s1.equals(s4));//true

            System.out.println(s2 == s4);//false
        }

结果

false
true
false
true
true
true
false

Process finished with exit code 0

以下是一个ArrayList中判断字符串相等的例子,最重要的是最后一个,可以看源码知道他们是如何比较的

public static void main(String[] args) {
            String str ="hello";
            List<Object> list = new ArrayList<>();
            list.add("hello");

            System.out.println(list.contains(str));
            System.out.println(list.contains("hello"));


            MyString myString = new MyString();
            List<MyString> list2 = new ArrayList<>();
            list2.add(myString);

            String str1 =null;
            System.out.println(list2.contains(str));

            myString.b=str;//這個是false
            System.out.println(list2.contains(str));
        }


        static class MyString {

            String b;

            @Override
            public boolean equals(Object obj) {
                return b.equals(obj);
            }
        }
public class ArrayList<E> extends AbstractList<E>
        implements List<E>, RandomAccess, Cloneable, java.io.Serializable
{
   public boolean contains(Object o) {
        return indexOf(o) >= 0;
    }

    public int indexOf(Object o) {
        if (o == null) {
            for (int i = 0; i < size; i++)
                if (elementData[i]==null)
                    return i;
        } else {
            for (int i = 0; i < size; i++)
                if (o.equals(elementData[i]))
                    return i;
        }
        return -1;
    }
}

结果

true
true
false
false

Process finished with exit code 0

参考:
equals和==的区别

相关文章

网友评论

      本文标题:equals和==的区别,字符串相等判定,Arraylist内容

      本文链接:https://www.haomeiwen.com/subject/mrkdcftx.html