Java Object class

Source: Internet
Author: User

I. Overview
The object class is the ancestor of all Java classes . Each class uses Object as a superclass. All objects, including arrays, implement methods of this class. In cases where the superclass is not explicitly given, Java automatically takes object as the superclass to define the class. You can use a variable of type object to point to an object of any type. The object class has a default constructor method Pubilc object (), which is called when the subclass instance is constructed.
Variables of the object class can only be used as common holders of various values. To do anything specific to them, you need to know their original types and type conversions. For example:
Object obj = new MyObject ();

MyObject x = (MyObject) obj;


Second, API preview
    Object()
default constructor Method
    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.
    GetClass() 
returns the run-time class of an object.
    hashcode() 
returns the hash code value of the object.
    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.


iii. Instructions for use of the method

equals() method: Used to test whether an object is equal to another object. Its implementation in the object class is to determine whether two objects point to the same piece of memory area. This test is not useful because the memory area is different, even for objects with the same content. If you want to test whether an object is equal, you need to override this method for a more meaningful comparison. For example

Class Employee{public    Boolean equals (object Otherobj) {        ///Quick test is the same object if        (this = = Otherobj) return true;        If an explicit argument is NULL, you must return False        if (otherobj = = null) reutrn false;        If the class does not match, it is not possible to equal        if (getclass () = Otherobj.getclass ()) return false;                Now know that Otherobj is a non-empty employee object        Employee other = (Employee) otherobj;        Test if all fields are equal        return name.equals (other.name)            && salary = = Other.salary            && Hireday.equals (Other.hireday);    }}

The Java language specification requires the Equals method to have the following characteristics:
Reflexivity: X,x.equals (x) should return true for any non-null reference value.
Symmetry: x.equals (y) should return true for any non-null reference value x and Y, if and only if Y.equals (x) returns True.
Transitivity: For any non-null reference value x, Y, and Z, if X.equals (y) returns true and Y.equals (z) returns True, then X.equals (z) should return true.
Consistency: For any non-null reference value x and Y, multiple calls to X.equals (Y) always return TRUE or always return false, provided that the information used in the Equals comparison on the object has not been modified.
X,x.equals (NULL) should return FALSE for any non-null reference value.
As seen here, the above example is the standard implementation of the Equals method of the Java specification, and it is recommended to implement the Equals method of the class using the above example.

toString (): Returns the string representation of the object. The ToString () method in the object class prints out the class name and the memory location of the object. Almost every class overrides the method to print a representation of the current state of the object. Most (not all) toString () methods follow the following format: class name [Field name = value, field name = value ...], of course, subclasses should define their own toString () method. For example:

Public String toString () {    Reurn "employee[name=" + name + ", salary=" + salary + ", hireday=" + Hireday + "]";}

The ToString () method is a very important debugging tool, and many classes in the standard class library define the ToString () method so that the programmer can get useful debugging information.

Java Object class

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.