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's the difference between = = and equals in Java

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

Share

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

In this article, the editor introduces in detail "what's the difference between Java = = and equals", the content is detailed, the steps are clear, and the details are handled properly. I hope this "Java = what's the difference between equals" article can help you solve your doubts, the following follows the editor's ideas slowly in depth, together to learn new knowledge.

= = is an operator of Java, and there are two ways to compare

For basic data types, the judgment of = = is whether the two boundary values are equal.

Public class DoubleCompareAndEquals {Person person1 = new Person (24, "boy"); Person person2 = new Person (24, "girl"); int c = 10; private void doubleCompare () {int a = 10; int b = 10; System.out.println (a = = b); System.out.println (a = = c); System.out.println (person1.getId () = = person2.getId ());}}

For reference types, = = determines whether the quotes on both sides are equal, that is, whether both objects point to the same memory area

Private void equals () {System.out.println (person1.getName (). Equals (person2.getName ());}

Equals is the parent class of any object in Java, which is the method defined by the Object class. Equals can only compare objects, indicating whether the values of both sides of the reference are equal. Keep in mind here that either = = compares or references are equal, while equals compares values, which need to be distinguished.

Equals for comparison between objects has the following characteristics

Reflexivity: x.equals (x) returns true for any non-null reference (x)

Symmetry: for any non-null references (x) and (y), if x.equals (y) is true, then y.equals (x) is also true

Transitivity: there are three values for any non-null reference value: X, y, and z, and if x.equals (y) returns true,y.equals (z) returns true, then x.equals (z) should also return true.

Consistency: for any non-null reference x and y, if x.equals (y) is equal, then they must always be equal.

Non-null: for any non-null reference value x, x.equals (null) must return false.

After reading this, the article "what's the difference between Java = = and equals" has been introduced. If you want to master the knowledge of this article, you still need to practice and use it yourself to understand it. If you want to know more about related articles, welcome to follow 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