One: Concept
Java.long.object
The object class is the ancestor of all Java classes. Each class uses Object as a superclass.
Two: Overview of the methodology
Clone ()creates and returns a copy of this object. equals (Object obj)indicates whether a different object is "equal" to this object. Finalize ()This method is called by the object's garbage collector when the garbage collector determines that there are no more references to the object. (Thegarbage collection mechanism that comes with the JVM can only reclaim new objects )getclass ()returns the run-time class of an object. hashcode ()returns the hash code value of the object. ( by default, Hashcode () in object returns the 32-bit JVM memory address of the objects.) That is, if the object does not override the method, 32 of the corresponding object is returned as the JVM memory address. )Notify ()wakes up a single thread waiting on this object monitor. Notifyall ()wakes all the threads waiting on this object monitor. toString ()returns the string representation of the object. Wait ()causes the current thread to wait until other threads call the Notify () method of this object or the Notifyall () method. Wait (long timeout)causes the current thread to wait until another thread calls this object's notify () method or the Notifyall () method, or exceeds the specified amount of time. Wait (long timeout, int nanos)causes the current thread to wait until another thread calls this object's notify () method or the Notifyall () method, or some other thread interrupts the current thread, or has exceeded an actual amount of time. Three: The specific use of the method1.equals () and "= ="in the object class,equals and "= =" Both implicitly consider the address of the object in the JVM to be compared. Because equals is achieved by "= =". (Different references must point to the same object)
Equals method source Code
publicbooleanreturn( this = = obj); }
The equals method is overridden in the string class : Compares the content pointed to by two object references for equality. (Can point to different objects with the same content in the object)
overriding the Equals method in the//string class
Public Booleanequals (Object anobject) {2if( This==anobject) { 3return true; 4 } 5if(AnObjectinstanceofString) { 6 String anotherstring =(String) anobject;7intn =value.length;8if(n = =anotherString.value.length) {9CharV1[] =value;10CharV2[] =Anotherstring.value;11inti = 0;12 while(n--! = 0) {13if(V1[i]! =V2[i])14return false;i++;16 }17return true;18 }19 }20return false;21}
2.equals () and hashcode ()the Equals () and Hashcode () methods are used for comparison purposes in the same class, especially if the objects in the container are stored in the same category as set to determine whether the object being placed is duplicated. equals () equal to two objects, hashcode () must be equal, equals () not equal to two objects, but does not prove that their hashcode () are unequal. when the Equals method is overridden, it is often necessary to override the Hashcode method to maintain the general contract of the hashcode method, which declares that the equality object must have an equal hash code.
//overriding the Hashcode () method in the String class Public inthashcode () {2inth = hash;//Default to 0 # # # # of private variables in the string class3if(h = = 0 && value.length > 0) {//private Final char value[]; # # # Sting array of string contents saved in class4CharVal[] =value;5 6 for(inti = 0; i < value.length; i++) { 7 h = * H +Val[i];8 } 9 hash =h;10 }11returnh;
12}
//A class that does not override the Hashcode method directly returns the address of a 32-bit object in the JVM; the long class overrides the Hashcode method, returning the computed hashcode value Public classcomhashcode{2 Public Static voidMain (string[] args)throwsException {3 Comhashcode A =NewComhashcode ();4 Comhashcode B =NewComhashcode ();5 System.out.println (A.hashcode ());//8709196966 System.out.println (B.hashcode ());//2987927207 8 Long NUM1 =NewLong (8); 9 Long num2 =NewLong (8);Ten System.out.println (Num1.hashcode ());//8System.out.println (Num2.hashcode ());//812 }13}
3.wait () and sleep ()
sleep is a method of the thread class, and wait is a method of the object class.
Sleep does not release resources, and wait releases resources.
Important methods in the Java.long.object class