Home> Java> JavaBase> body text

Analysis on == and equals in java

王林
Release: 2019-11-27 16:21:07
forward
1730 people have browsed it

Analysis on == and equals in java

== Analysis

1. For basic data types, what is compared is whether their specific contents are the same, and their memory The address is irrelevant.

For example:

public class Test{ public static void main(String[] args) { int i = 10; double j = 10.0; float m = 10.0f; char k = 10; boolean b = true; System.out.println(i == j); System.out.println(i == m); System.out.println(m == k); System.out.println("*******************"); //System.out.println(i == b); 很明显,布尔型不能和其他基本数据类型比较,编译报错. System.out.println(System.identityHashCode(i)); System.out.println(System.identityHashCode(j)); System.out.println(System.identityHashCode(m)); System.out.println(System.identityHashCode(k)); System.out.println(System.identityHashCode(b)); } }
Copy after login

Output:

true true true ******************* 366712642 1829164700 2018699554 1311053135 118352462
Copy after login

Recommended related video tutorials:java free video tutorial

Analysis:

① According to the comparison results and memory addresses, the contents are the same but the memory addresses are different, but the returned results are all true, which means that when comparing, we only look at whether the contents stored in the variables are the same, and It has nothing to do with memory address.

②==The data on both sides of the comparison need to be forced to be converted to the same type. For example, the comparison between int and double will be forced to double.

③The Boolean type cannot be compared with basic data types such as int, float, double, char, etc., and an error will be reported during compilation.

2. Comparison of reference data types:

public class Test{ public static void main(String[] args) { Test t1 = new Test(); Test t2 = new Test(); String name1 = "琼宝"; String name2 = "琼宝"; System.out.println(name1 == name2); // true System.out.println(System.identityHashCode(name1)); //366712642 System.out.println(System.identityHashCode(name2)); //366712642 System.out.println(t1 == t2); //false System.out.println(System.identityHashCode(t1)); //366712642 System.out.println(System.identityHashCode(t2)); //1829164700 System.out.println(System.identityHashCode(new Test())); //2018699554 System.out.println(System.identityHashCode(new Test())); //1311053135 System.out.println(new Test() == new Test()); } }
Copy after login

Output:

true 366712642 366712642 false 366712642 1829164700 2018699554 1311053135 false
Copy after login

Analysis:

①For reference data types, at this time It involves two pieces of memory, and the comparison is whether the memory addresses are the same. For example, when executing the statement:

Test t1 = new Test() Test t2 = new Test(), it will Open up two pieces of memory, one for storing t1 t2 and one for storing two new Test(). It is obvious that the memory addresses of t1 and t2 are different, and even the addresses of the two new Test() are different, so when using == to compare at this time, the result must be false.

(Supplement: What is stored in t1 and t2 is actually the first address of the memory used by the new Test() object.)

② For the String type, when the content is the same, the The memory addresses are also the same, and the addresses are also looked at when comparing.

If you want to compare whether the contents stored in the object are the same (not comparing addresses), then == cannot be implemented, and equals is required.

Analysis of equals

1. Let’s look at an example of equals comparison:

public class TestEquals { public static void main(String[] args) { TestEquals t1 = new TestEquals(); TestEquals t2 = new TestEquals(); System.out.println(t1.equals(t2)); //false String s1 = new String(); String s2 = new String(); System.out.println(System.identityHashCode(s1)); System.out.println(System.identityHashCode(s2)); System.out.println(System.identityHashCode(new String())); System.out.println(System.identityHashCode(new String())); System.out.println(s1.equals(s2)); // true } } 输出结果: false 366712642 1829164700 2018699554 1311053135 true
Copy after login

2. Analysis:

①t1 and The comparison of t2 is false, and the comparison of s1 and s2 is true. This involves the rewriting of the equals method. First, look at the source code of equals in the Object class:

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

② For s1.equals(s2) , this is s1, obj is s2, and the comparison in the source code uses ==, which is obviously a comparison of reference data types, looking at the memory addresses of s1 and s2. The above analysis already knows that their addresses are different. So the result is naturally false.

③The comparison result between s1 and s2 is true. This is because in the String class, the equals method has been rewritten. See the source code:

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; }
Copy after login

④Passed Comparing it with the equals source code in object, we can see that the equals method in String no longer compares the address of the object, but looks at the contents of the two objects, or whether the attributes of the two objects are the same.

⑤Classes such as String, Date, File... etc. all rewrite the equals method.

Summary:

1.==When used to compare basic data types, what is compared is whether the contents are equal. When comparing reference data types , it depends on whether the memory addresses are equal.

2.equals can only compare reference data types (objects). Before being rewritten, == is used to compare memory addresses. After rewriting, the comparison is whether the specific content and attributes of the objects are consistent.

For more related articles and tutorials, please visit:Introduction to java programming

The above is the detailed content of Analysis on == and equals in java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!