In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/03 Report--
The elements in Set cannot be repeated, and the equals () method is used to determine whether the elements are repeated or not.
The equals () and = = methods determine whether the reference value points to the same object equals () is overridden in the class in order to return a true value if the content and type of the two separate objects match.
The difference between equals () and =
The = = operator is specifically used to compare whether the values of two variables are equal, that is, to compare whether the values stored in memory corresponding to variables are the same, and to compare whether two basic types of data or two referenced variables are equal. You can only use the = operator. If the data pointed to by a variable is of object type, then two blocks of memory are involved, the object itself occupies a piece of memory (heap memory), and the variable also takes up a piece of memory, such as Objet obj = new Object (); the variable obj is one memory, and new Object () is another memory, so the value stored in the memory corresponding to the variable obj is the first address of the memory occupied by the object. For variables that point to the object type, if you want to compare whether two variables point to the same object, that is, to see whether the values in memory corresponding to the two variables are equal, you need to use the = = operator to compare. The equals method is used to compare whether the contents of two independent objects are the same, just as to compare whether two people look the same. The two objects it compares are independent.
For example, for the following code:
String a=new String ("foo"); String b=new String ("foo")
Two new statements create two objects, and then point to one of the objects with the variables of an a.equals b, which are two different objects, whose first addresses are different, that is, the values stored in an and b are different, so the expression axionomb will return false, and the contents of the two objects are the same, so the expression a.equals (b) will return true.
In actual development, we often have to compare whether the contents of the passed string are equal, for example, String input=. ; input.equals ("quit"), many people use = = to make a comparison without paying attention, which is wrong. If you look at a few actual teaching videos of projects on the Internet, there are a lot of such mistakes. Remember, string comparisons basically use the equals method. If a class does not define its own equals method, it inherits the equals method of the Object class. The implementation code of the equals method of the Object class is as follows:
Boolean equals (Object o) {return this==o;}
This means that if a class does not define its own equals method, its default equals method (inherited from the Object class) is to use the = = operator and to compare whether the object pointed to by the two variables is the same object, then using equals and using = = will get the same result, and always return false if you compare two separate objects. If you write a class that wants to compare whether the contents of the two instance objects created by this class are the same, then you must override the equals method and write your own code to determine when the contents of the two objects can be considered to be the same, as can be seen above, as summarized as follows:
= =:
Basic type: compare whether the value is the same or not
Reference type: compare whether the address value is the same
Equals ():
Reference type: by default, the address value is compared, which can be overridden, and whether the member variable value of the object is the same.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.