Java-object class

Source: Internet
Author: User

The object class is the parent class of all classes, and if a class is not explicitly identified with the extends keyword to inherit a class, then the class inherits the object class by default

The method of the object class is suitable for all subclasses

1.toString () method

The method returns the hash code (object address string) of the object, which is used to differentiate the object, which, like a social security number, distinguishes everyone

Man Man=new Mans ();

System,out.println (Man.tostring ());

Of course, the ToString () method is also overridden to represent the properties of the object

public void toString () {

Return "rewrite ToString method";

}

Equals () compares whether the object's reference is the same address

However, in general, if the two values of the object are the same, they need to be overridden when the parent class is not satisfied

Let's look at the first type:

public static void Main (string[] args) {

Dog Dog1=new Dog ();

Dog Dog2=new Dog ();

if (Dog1.equals (DOG2)) {

System.out.println ("Two objects are the same");

}else{

System.out.println ("Two objects are not the same");

}

}

The result must be different, because two objects open up different addresses, even if the property values of the two objects are the same.

2. To compare the values of two objects, you need to override the Equals method

The specific equals and = = relation refer to the following URL

Http://www.cnblogs.com/dolphin0520/p/3592500.html

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.