Java Common class---Object

Source: Internet
Author: User

Object class

  The object class is the parent class of all Java classes, which are located in the Java.lang package. Any Java object that does not have a display definition parent class then it defaults to the object class as the parent class.

Here's how:

There are 5 methods associated with threading: Notify (), Notifyall (), wait (), wait (long timeout), wait (long Timeout,int Nanos), which are explained when the thread is introduced.

Here are some other ways to see:

(1) equals (Object obj) method
function:

equals method implements an equality relationship on a non-null object reference:
reflexivity: For any non-null reference value x, X.equals (x) should return true
symmetry: for any non-null reference value x and Y, when and only if Y.equals (x) returns True , X.equals (Y) returns true
transitivity: for any non-null reference value x, Y, Z, if X.equals (y) returns True, and Y.equals ( Z) returns True, then X.equals (z) should return true
consistency: multiple calls to X.equals (y) for any non-null reference value X and Y Always returns TRUE or always returns FALSE.

 1  public  boolean   equals (Object obj) { 2  return  (this  == obj);  3 } 

The Equals method relates to the "= =" Operator:
(a) The Equals () method can only compare reference types; "= =" can compare reference types and basic types.
(b) The default Equals () method compares the memory address, which is basically equivalent to "= =", but is the value compared to the class file, String, date, and wrapper class instead of the memory address.
(c) "= =" when comparing, reference type data comparison is reference, and memory address; the base data type compares values.
Overriding the Equals method:
The general steps to override the Equals method are as follows:
(a) Use = = to check if the parameter is a reference to this object
(b) Determine if the object to be compared is NULL, or False if it is null
(c) Use if (getclass () = Obj.getclass ()) to determine whether the parameter is of the correct type
(d) Converting parameters to the appropriate type
(f) Compare whether object property values match
The following is the student class overriding the Equals method to compare objects for equality, and the general Euqals method can be generated automatically by the Eclipse tool.

1  Public classStudent {2     PrivateString ID;3     PrivateString name;4     PrivateString age;5     PrivateString Home;6 @Override7      Public Booleanequals (Object obj) {8         if( This==obj)9             return true;Ten         if(obj = =NULL) One             return false; A         if(GetClass ()! =Obj.getclass ()) -             return false; -Student other =(Student) obj; the         if(age = =NULL) { -             if(Other.age! =NULL) -                 return false; -}Else if(!age.equals (other.age)) +             return false; -         if(Home = =NULL) { +             if(Other.home! =NULL) A                 return false; at}Else if(!home.equals (other.home)) -             return false; -         if(id = =NULL) { -             if(Other.id! =NULL) -                 return false; -}Else if(!id.equals (other.id)) in             return false; -         if(Name = =NULL) { to             if(Other.name! =NULL) +                 return false; -}Else if(!name.equals (other.name)) the             return false; *         return true; $     }Panax Notoginseng}

In general, overriding the Equals method should cover the Hashcode method

(2) Hashcode () method
Hashcode is based on a certain algorithm to obtain a value, is the object's hash code value. Mainly used in the collection to achieve quick find operations.
The rules for hashcode in Java are as follows:
(a) During Java application execution, when the Hashcode method is called multiple times on the same object, the same integer must be returned consistently, provided that the information used to compare the object to equals is not modified.
(b) If two objects are equal according to the Equals (object) method, calling the Hashcode method on each object in both objects must produce the same integer result.
(c) If two objects are not equal according to the Equals (object) method, calling the Hashcode method on the kernel object in both objects does not require that a different integer result be generated.
Simply put: If two objects equals true, then their hashcode values must be the same, and if two objects have the same hashcode, they will not necessarily be true.

The following is the student class override Hashcode method, which can be generated automatically through the Eclipse tool.

1 @Override2      Public inthashcode () {3         Final intPrime = 31;4         intresult = 1;5result = Prime * result + ((age = =NULL) ? 0: Age.hashcode ());6result = Prime * result + (home = =NULL) ? 0: Home.hashcode ());7result = Prime * result + ((id = =NULL) ? 0: Id.hashcode ());8result = Prime * result + ((name = =NULL) ? 0: Name.hashcode ());9         returnresult;Ten}

(3) toString () method
The ToString () method is another important method defined in the object class whose source code is:

1  Public String toString () {2return getclass (). GetName () + "@" + integer.tohexstring (Hashcode () ); 3 }   

The return value of the method is a string type that describes the information about the current object. The ToString () method implemented in the object class returns the type and memory address information for the current object, but is overridden in some subclasses (such as String,date), and optionally overrides the ToString () method in a user-defined type to return the required information.

The following is the student class override ToString method, which can be generated automatically through the Eclipse tool.

1     @Override 2      Public String toString () {3         return "Student [id=" + ID + ", name=" + name + ", age=" + age4                 + ", home=" + Home + "]"; 5     }

--------------------------------------------------------------------------------------------------------------- --------------------------------------------------------

Java Common class---Object

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.