Java Super class-java.lang.object

Source: Internet
Author: User

Java is object-oriented and object is a superclass of all objects (not inheritance or implementation interface)

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.

If you

What is the object classEditThe 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; method previewEditThe object () default constructor method Clone () creates and returns a copy of this object. Equals (Object obj) indicates whether some other object is "equal" to this object. Finalize () This method is called by the object's garbage collector when the garbage collector determines that no more references to the object exist. 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 up 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 the Notify () method of this object 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. Method usage InstructionsEdit1. Equals () method: Used to test whether an object is equal to another object. Its implementation in the object class is to determine whether the values of two objects are equal. This test is not useful because the memory area is different, even for objects of 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{...//This example comes from the Java Core Technology volume a public boolean equals (object Otherobj) {//Quick test is the same object if (this = = Otherobj) Return true;//If the explicit argument is null, you must return Falseif (otherobj = = null) return false;//if the class does not match, it cannot be equal if (getclass ()! = Otherobj.getclass ()) return false;//now know that Otherobj is a non-empty employee object, Employee other = (employee) otherobj;// Test all fields for equality return name.equals (othername) && salary = = other.salary&& hireday.equals (other.hireday);}} AddEditThe 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.

Java Super class-java.lang.object

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.