Override of the object class ToString () and Equals ()

Source: Internet
Author: User

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 ()

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.