Basic function:
The Objiect class is the parent class for all classes.
When any class is defined, the default parent class is the object class if the parent class is not explicitly defined.
Class A extends object{}
Throughout Java, the inheritance of classes persists. For example:
String class
At this point a benefit arises, using the object class to accept all classes.
Object obj = new book ();//Upward transformation
Obect obj = "Hello";//Upward transformation
Using object is the best choice if you do not know the parameter type
http://docs.oracle.com/javase/7/docs/api/
You can see that you define an parameterless construction method inside the object class.
What is this for??
Because the object class is the parent class for all classes, it is bound to call the parent class's parameterless constructor by default when the objects are instantiated.
He also offers a lot of methods. These methods are important.
In a strict sense (generally non-compliance), any simple Java class should overwrite the three methods of the object class
1.toString ()
2.equals (object); objects comparison method
3.public int Hashcode ()
First method: Get object information
ToString ():
Example:
class extends Object {} Public class objecttest{ publicstaticvoid main (String arg[]) { New Book (); = "Hello" ; System.out.println (b); System.out.println (B.tostring ()); System.out.println (s); }}
Observe that the result of the output is the same as the first row and the second row.
This is because when the object is output, the ToString () method is automatically called to turn the object into a string output;
Therefore, in the future when using the object class to overwrite the ToString () method;
classBook {PrivateString title; Private DoublePrice ; PublicBook (String title,DoublePrice ) { This. title =title; This. Price =Price ; } PublicString toString () {return"title" + This. Title + ", Price:" + This. Price; }} Public classobjecttest{ Public Static voidMain (String arg[]) {Book B=NewBook ("Java", 19.9) ; String s= "Hello" ; System.out.println (b); System.out.println (B.tostring ()); System.out.println (s); }}
The ToString () method is rewritten at this point. You can output objects directly.
Second: Object comparison
Equalls ();
Public Booleanequals (Object obj) {if( This==obj)return true ; if(obj = =NULL) return false ; if(objinstanceofBook )//Determine if the book type is instantiatedreturn false ; Book Book=(book) obj; if( This. Title.equals (Book.title) && This. Price = =Book.price)return true ; }
Override of the object class ToString () and Equals ()