In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "the comparative analysis of the examples of objects in java". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn the comparative analysis of the examples of objects in java.
Comparison of objects of the same type
Three dimensions to compare
Identity
Equality
Similarity.
Sample introduction
Imagine a scene like this: Xiao Wang went to the library and borrowed a java core technology volume 1, as shown in the picture
Unfortunately, Xiao Wang lost his book and bought two more java core technology volumes 1.
New book 1 new book 2
If Xiao Wang uses the newly bought book 1 to return the book
1, if the library forbids him to return the book / / that is, he does not have identity (not the same book)
2, if the library allows him to return books / / have "equality" (not the same book, as long as equal)
If Xiao Wang uses the newly bought book 2 to return the book
3, if the library allows him to return books / / have similarity
Book b1 = new Book ("white java core volume 1"); identity relationship between b1 and b1
Book b2 = new Book ("White java Core Volume 1"); o1==o2 true
B1 = = b2 = > false
B1 = = b3 = > false
Equality judgment
There is no native logic for similarity judgment in java, but there is a constraint for equality judgment, that is, equals (equality).
There is an equals method from the Object class. + all classes inherit from Object. All classes have their own equals methods
Java wants the implementer of the class (who defined the class) to write the correct equals method to ensure equality.
Hope:
B1.equals (b1) = > true
B1.equals (b3) = > false
B1.equals (b2) = > true
Public class Main {public static void main (String [] args) {Book b1 = new Book ("white", "java core volume 1"); Book b2 = new Book ("white", "java core volume 1"); Book b3 = new Book ("yellow", "java core volume 1"); / / View identity System.out.println (b1==b1); / / true System.out.println (b2==b2) / / true System.out.println (b3==b3); / / true System.out.println (b1==b2); / / false System.out.println (b1==b3); / / false / / View equivalence System.out.println (b1.equals (b1)); / / true System.out.println (b1.equals (b2)); / / hope that true// actually prints false System.out.println (b1.equals (b3)) / / false}}
In the above example, b1.equals (b2) is false because equals is not overridden in Book, so the equals method of its parent class Object is executed, while in Object, equals is still judging identity, so the result naturally shows false.
Method if you want b1.equals (b2) to be true, if you need to rewrite the equals method of Book correctly.
The correctness must be guaranteed after rewriting the method.
What is correctness:
Reflexivity = > b1.equals (b1) must be true
When b1.equals (b2) = > true; b2.equals (b2) = > true
Transitivity
B1.equals (b2) = > true & & b2.equals (b3) = > true available b1.equals (b3) = > true
4, and the result of null is generally false
B1.equals (null) = > false
You don't need to write it manually here, just use the tool to generate the correct equals.
Code-> Generate-> equals ()
Public class Book {@ Override public boolean equals (Object o) {if (this = = o) return true; if (o = = null | | getClass ()! = o.getClass () return false; Book book = (Book) o; return color.equals (book.color) & & name.equals (book.name);} public String color; public String name Public Book (String color, String name) {this.color = color; this.name = name;}
After rewriting the equals method, run it again, where the equals method has been rewritten and b1.equals (b2) is true.
At this point, I believe you have a deeper understanding of the "comparative analysis of examples of objects in java". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.