In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
In this article Xiaobian for you to introduce in detail "Java Object class method case analysis", the content is detailed, the steps are clear, the details are handled properly, I hope this "Java Object class method instance analysis" article can help you solve your doubts, following the editor's ideas slowly in-depth, let's learn new knowledge.
Introduction of equals method in 1.equals method 1.API
2. Compare = comparison with equals
= = you can judge not only the basic type, but also the reference type. if you judge the basic type, you can judge whether the value is equal, and if you judge the reference type, you can judge whether the address is equal, that is, whether it is the same object.
The equals method is a method in the Object class, which can only determine the reference type. The default is to determine whether the address is equal. This method is often overridden in the subclass and is used to determine whether the content is equal after rewriting.
We can take a look at the JDK source code of the equals method in Object,String and Integer.
Object o = new Object (); o.equals (1); / equals method System.out.println in the Object class ("hello" .equals ("hello")); / / equals method Integer integer in String = new Integer (5); System.out.println (integer.equals (5)); / / equals method in / / Integer / / place the cursor over the equals method and press Ctrl while pressing the left mouse button to jump to the JDK source code of the equals method in String and Integer. / / equals method public boolean equals (Object obj) {return (this = = obj) in the Object class; / / determine whether it is the equals method public boolean equals (Object anObject) {if (this = = anObject) {return true overridden in the current object} / / String class. / / first determine whether the current object} if (anObject instanceof String) {/ / determine whether the current type is a string type String anotherString = (String) anObject;// downward transformation int n = value.length;// save the length of the incoming string if (n = = anotherString.value.length) {char v1 [] = value Char v2 [] = anotherString.value; int I = 0; while (NMI -! = 0) {if (v1 [I]! = v2 [I]) / / An one-character comparison of return false; iTunes;} return true }} return false;} / / equals method overridden in the Integer class public boolean equals (Object obj) {if (obj instanceof Integer) {/ / determine whether the current type is the Integer type return value = = ((Integer) obj). IntValue (); / / downward transformation} return false;} 2.hashCode method
Introduction of hashCode method in 1.API
two。 Improve the efficiency of containers with hash structures.
3. If two references point to the same object with the same hash value, and point to different objects, the hash value will be different.
4. The hash value is mainly based on the address, but the hash value cannot be equivalent to the address.
3.toString method
Introduction of toString method in 1.API
two。 Default return: full class name (package name + class name) + @ + hexadecimal hash value.
3. The toString method is often overridden to return object properties.
4. When the object is output directly, the toString method is called by default.
/ / toString method source code public String toString () {return getClass () .getName () + "@" + Integer.toHexString (hashCode ()) in JDK / / return full class name (package name + class name) + @ + hash hexadecimal} / / override the toString method @ Override public String toString () {return "Person {" + "name='" + name +'\'+ ", age=" + age + ", gender=" + gender +'}' } / / often overrides the toString method to return object properties. 4.finalize method
Introduction of finalize method in 1.API
two。 When an object is reclaimed, the system automatically calls the finalize method of the object. Subclasses can override this method to do something to release resources.
3. When an object has no reference, jvm uses the garbage collection mechanism to destroy the object, and the finalize method is called before destroying the object.
4. The call of garbage collection mechanism is determined by the GC algorithm of the system, and can also be triggered actively by System.gc ().
After reading this, the article "Java Object method instance Analysis" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself to understand it. If you want to know more about related articles, you are 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.