Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the difference between using = = and equals in Java

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Most people do not understand the knowledge points of this article "what is the difference between using = = and equals in Java", so the editor summarizes the following content to you, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this article "what is the difference between using = = and equals in Java".

1. = = parse

= = is often used for comparison between the same basic data types, and can also be used for comparison between objects of the same type

If you are comparing basic data types, you are comparing whether the values of the two basic data types are equal.

If = = is a comparison of two objects, then comparing the references of two objects is to compare whether the references of the two objects are equal, that is, to determine whether the two objects point to the same memory area.

2. Analysis of equals method

The equals method is mainly used between two objects to detect whether one object is equal to the other.

Let's take a look at the source code of the equals method in the Object class

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

Its function is also to determine whether two objects are equal, and there are generally two uses:

Case 1: the equals method of an object is not overridden, so when equals is called, it compares whether the references of the two objects are equal, that is, whether the two objects point to the same memory area. At this point, it is equivalent to comparing two objects.

Dog class

Package com.xiao;/** * @ author: Xiao Xiao * @ date: Created in 14:42 on 2022-3-11 * / public class Dog {private String name; private Integer age; public Dog () {} public String getName () {return name;} public void setName (String name) {this.name = name;} public Integer getAge () {return age } public void setAge (Integer age) {this.age = age;} public Dog (String name, Integer age) {this.name = name; this.age = age;}}

Test class

Import com.xiao.Dog;public class Test {public static void main (String [] args) {Dog dog = new Dog ("Xiao Wang", 2); Dog dog1 = new Dog ("Xiao Wang", 2); System.out.println (dog.equals (dog1));}}

Test result

False

Case 2: the object's equals method is overridden. In general, our overridden equals method compares whether the contents of two objects are equal. If equal, return true, otherwise return false.

Dog class

Package com.xiao;/** * @ author: Xiao Xiao * @ date: Created in 14:42 on 2022-3-11 * / public class Dog {private String name; private Integer age; public Dog () {} public String getName () {return name;} public void setName (String name) {this.name = name;} public Integer getAge () {return age } public void setAge (Integer age) {this.age = age;} public Dog (String name, Integer age) {this.name = name; this.age = age;} @ Override public boolean equals (Object obj) {if (obj.getClass ()! = getClass ()) {return false;} Dog dog = (Dog) obj If (dog.getAge () = = age & & dog.getName () .equals (name)) {return true;} return false;}}

Test result

The test class code is the same as above

True

3. The equals method has the following characteristics.

Reflexivity: for any reference value x that is not null, x.equals (x) must be true.

Symmetry: for any reference values x and y that are not null, y.equals (x) is also true if and only if x.equals (y) is true.

Transitivity: for any reference values x, y, and z that are not null, if x.equals (y) is true and y.equals (z) is true, then x.equals (z) must be true.

Consistency: for any reference values x and y that are not null, if the object information used for equals comparison has not been modified, x.equals (y) either consistently returns true or false when called multiple times.

Returns false for any reference value that is not null. Equals (null).

The above is about the content of this article on "what is the difference between using = = and equals in Java". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more related knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report