Java course 17th (Application of Object and some of its methods)

Source: Internet
Author: User

Java course 17th (Application of Object and some of its methods)
Object: root class of all classes

Objects are constantly extracted and have the relational content of all objects.

Method Abstract:

Clone (): Create and return a copy of this Object equals (Object obj): Indicates whether other objects are "equal" to this Object finalize (): when the Garbage Collector determines that there is no more reference to this object, the object's garbage collector calls this method getClass (): returns the runtime class hashCode () of Objext (): returns the hash value of this object y (): Wake up a single thread waiting on this object monitor notifyAll (): Wake up all threads waiting on this object monitor toString (): returns the string of this object to wait (): Before other threads call the notify () or notifyAll () methods of this object, the current thread waits for wait (long timeout ): before other threads call the notify () or notifyAll () method of this object or exceed the specified time, the current thread waits for wait (long timeout, int nanos ): the current thread waits until another thread calls the notify () or notifyAll () method of this object, or another thread breaks the current thread, or exceeds the actual time.

I. equals (): (important)

Class Man {private int age = 3; Man (int age) {this. age = age ;}} class Son {}public class Main {public static void main (String [] args) {Man BLF = new Man (20 ); man BLF1 = new Man (20); Man BLF2 = BLF1; System. out. println (BLF = BLF1); // falseSystem. out. println (BLF. equals (BLF1); // flase // Why is it false? // Once one Man (20) passes the age and the other MAN ("xxx") passes the name, the two must be different. out. println (BLF1.equals (BLF2); // trueSon BLF5 = new Son (); System. out. print (BLF2.equals (BLF5); // objects of different class definitions can also be compared }}

Of course, in many cases, equals is not enough to judge the address, and can be rewritten.
Generally, this method is overwritten. Based on the specific content of the object, the basis for determining whether the object is equal

Import java.net. interfaceAddress; import javax. management. runtimeErrorException; class Man extends Object {private int age = 3; Man (int age) {this. age = age;} // compare public boolean cmp (Man B) {return this. age = B. age;} // But Man inherits from Objext. If Objext has the equals method, there is no need to write the cmp repeated method. // rewrite the repeated spublic equals (Object B) of the parent class) // polymorphism, upward transformation {if (! (B instanceof Man) {// return false; // or warning Type Error throw new RuntimeException ("Do not deliberately find the fault, only judge the person");} Man c = (Man) b; // return this. age = c. age ;}} class animol {}public class Main {public static void main (String [] args) {Man BLF = new Man (20 ); man BLF2 = new Man (20); System. out. println (BLF. cmp (BLF2); animol BLF3 = new animol (); System. out. println (BLF. equals (BLF3 ));}}



Ii. hashCode () // hash, which will be important in the Collection framework

Import java.net. interfaceAddress; import javax. management. runtimeErrorException; class Man extends Object {private int age = 3; Man (int age) {this. age = age ;}} class animol {}public class Main {public static void main (String [] args) {Man BLF = new Man (20 ); man BLF2 = new Man (20); System. out. println (BLF); System. out. println (BLF. hashCode (); // decimal System. out. println (Integer. toHexString (BLF. hashCode ()));}}

In actual development, you may want to define the object's hash value, so the hashCode needs to be rewritten.

Import java.net. interfaceAddress; import javax. management. runtimeErrorException; class Man extends Object {private int age = 3; Man (int age) {this. age = age ;}public int hashCode () {return age ;}} public class Main {public static void main (String [] args) {Man BLF = new Man (20 ); man BLF2 = new Man (20); System. out. println (BLF); System. out. println (BLF. hashCode (); // in decimal format, the hexadecimal value of 20 is 14System. out. println (Integer. toHexString (BLF. hashCode ()));}}



3. getClass Method

When creating an object, first load the Man. class bytecode file object in the memory, and then new Man (); each object has its own bytecode file object.

Import java.net. interfaceAddress; import javax. management. runtimeErrorException; class Man {private int age = 3; Man (int age) {this. age = age ;}public int hashCode () {return age ;}} public class Main {public static void main (String [] args) {Man BLF = new Man (20 ); man BLF2 = new Man (20); Class c = BLF. getClass (); Class d = BLF2.getClass (); System. out. println (c); System. out. println (c = d); // true, the two object's bytecode file objects are the same // The Class name System obtained by using the getName method in the Class. out. println (c. getName (); // Man }}


4. toString method:


Import java.net. interfaceAddress; import javax. management. runtimeErrorException; class Man {private int age = 3; Man (int age) {this. age = age ;}} public class Main {public static void main (String [] args) {Man BLF = new Man (20); Man BLF2 = new Man (20 ); system. out. println (BLF); // Man @ 15db9742, @ is previously getName, @ is later hashCode to get System. out. println (BLF. toString (); // actually, BLF is omitted. toString () System. out. println (BLF. getClass (). getName () + "----" + Integer. toHexString (BLF. hashCode ()));}}

We recommend that you override this method for all subclasses.

import java.net.InterfaceAddress;import javax.management.RuntimeErrorException;class Man {private int age = 3;Man(int age){this.age = age;}public String toString(){return "Man"+age;}}public class Main{public static void main(String[] args){Man BLF = new Man(20);Man BLF2 = new Man(20);System.out.println(BLF);//Man20}}

PS: there are some other methods to complete when the dregs learn multithreading.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.