In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly shows you "how to rewrite equals in String". The content is simple and clear. I hope it can help you solve your doubts. Let me lead you to study and learn this article "how to rewrite equals in String".
Equals method as the Object class definition method, Object is also the parent class of all classes, which also includes String, String override equals method, let's see how to rewrite it.
In the first step, we judge whether the quotation marks of the two strings to be compared are equal. If the quotation marks are equal, the quotation marks are returned directly to true. If they are not equal, continue the following judgment.
The second part determines whether the object has an instance of String, and returns false if not. If so, the length of the two strings is compared to see if they are equal, and there is no need to compare them again. If equality compares whether the characters of two strings are equal, if one is not equal, false is returned.
Point of doubt:
If (this = = anObject) {return true;}
How does the judgment statement return true? Does the string compare the heap space? The String.intern () method indicates that the concept is different in different JDK versions.
After JDK1.7, the intern method determines whether the running constant pool has the specified string, and if not, adds the string such as the constant pool and returns its object.
Private void StringOverrideEquals () {String S1 = "aaa"; String S2 = "aa" + new String ("a"); String S3 = new String ("aaa"); System.out.println (s1.intern (). Equals (S1)); System.out.println (s1.intern (). Equals (S2)); System.out.println (s3.intern (). Equals (S1));} above is all the content of the article "how to rewrite equals in String". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, 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.
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.