Java在比较字符串的时候,equals 与== 完全一样呀?
阿神
阿神 2017-04-17 17:36:04
0
9
465

完全一样,比较字符串equals与== 完全没有区别吗?

阿神
阿神

闭关修行中......

reply all(9)
Ty80
        String a1="a";
        String a2="a";
        String b1=new String("b");
        String b2=new String("b");
        System.out.println(a1 == a2);
        System.out.println(a1.equals(a2));
        System.out.println(b1 == b2);
        System.out.println(b1.equals(b2));

Result:

true
true
false
true

== compares whether the variables pointed to objects are consistent. The reason why s and s2 are equal is because jvm optimizes and the two variables point to one object.
equal compares whether the strings stored in the object are the same.

刘奇

Some questions actually have the best answers in the source code

public boolean equals(Object anObject) {
    //如果引用的是同一个对象,返回真
    if (this == anObject) {
        return true;
    }
    //如果不是String类型的数据,返回假
    if (anObject instanceof String) {
        String anotherString = (String) anObject;
        int n = value.length;
        //如果char数组长度不相等,返回假
        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;
刘奇

The

== operator tests whether two references point to the same object. If you want to test whether two different objects are equal, you must use the equals() method.

If you are interested in learning more, you can read an article I wrote. Java Quick Literacy Guide

迷茫

I think your question is very problematic. How did you get the conclusion described in the text from the code you posted?
I’m going to make a rough guess at your intention and try to answer it reluctantly.
In java, == and equal methods are different, a brief description is as follows:

  1. ==

    == is used to determine whether two references point to the same object, such as
    Object a = new Object();
    Object b = a;
    a == b; // ==> true

  2. equal

    The

    equal method is used to determine whether two objects are equal. This method has been defined in the top-level class Object. The implementation of this method in Object uses == to compare references for equality. If any subclass wants to use the equal method, it is best to override the equal method of Object and provide its own equality logic. Define a Person class as follows.

public class Person {
    private String name;
    private int age;
    // ...
    @Override
    public boolean equal(Object o) {
        if(o instanceof Person) {
            return o.getName().equal(name) && o.getAge() == age;
        } else {
            return false;
        }
    }
}
巴扎黑

ss2 都是直接引用的常量 "12",编译器会优化代码,只创建一个 "12" String 对象,由两个变量来引用,所以 s == s2。但是如果你的另一个 "12" 是由其它方式创建的,比如 new String("12"),或者 "1234".substring(0, 2),你就会发现 == 不好使了,而 equals 是对内容进行比较。看 equals 的原码你会发现它也是先使用 == Comparatively quoted

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;
}
巴扎黑

It will be different when new comes out

迷茫

First of all, we must consider the functions of equals and ==:
== is equivalent to comparing the references of two objects, and the equals method is defined in the Object class, and the String class has rewritten it. The source code can be seen in the analysis upstairs. , the references are also compared using == first, and then the contents are compared.
When we compare two strings, we mostly want to compare the contents, so we use the equals method. If you use ==, the IDE will actually throw a warning.

So why do your == and equals methods have the same effect? ​​It depends on the difference between using "=" to create a string object and using new to create a string.
I don’t know if you have ever understood the string pool. My understanding is not deep. You can check it out for yourself if necessary.
The first time you use "=" to create a string object, you will check whether there is "12" in the string pool. If not, add one to the pool and return the reference to s; when you create s2 later, If it is found that there is one in the pool, then this reference is directly assigned to s2, so the references of s and s2 are the same, causing == to compare to true.
You can use new to create a string and see the effect:

        String s1 = "12";
        String s2 = "12";
        String s3 = new String("12");

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

When using the new keyword to create a string object, a new object will be created each time and the reference will be assigned to the variable.

Peter_Zhu

They all said it well, I suggest you search the string constant pool, it will give you a deep understanding

巴扎黑

The content of your s1 and s2 is the same,
Your assignment method is like this,
First, s1 will put the string "..." into the constant of the JVM virtual machine In the pool,
when assigning s2 for the second time, it will first determine whether the constant pool contains this string, and if so, point to it.
So even if you use == it will be equal.
If you use new string(123) in s2, then the reference addresses are different and they are not equal.
Code on mobile phone is not easy.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template