== 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.
== 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:
==
== is used to determine whether two references point to the same object, such as Object a = new Object(); Object b = a; a == b; // ==> true
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;
}
}
}
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:
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.
Result:
== 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
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:
==
== is used to determine whether two references point to the same object, such as
Object a = new Object();
Object b = a;
a == b; // ==> true
equal
Theequal 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.
s
和s2
都是直接引用的常量 "12",编译器会优化代码,只创建一个 "12" String 对象,由两个变量来引用,所以 s == s2。但是如果你的另一个 "12" 是由其它方式创建的,比如new String("12")
,或者"1234".substring(0, 2)
,你就会发现==
不好使了,而equals
是对内容进行比较。看equals
的原码你会发现它也是先使用==
Comparatively quotedIt 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:
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.
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.