Introduction to the object class in Java

Source: Internet
Author: User

1. The object class is the base class for all Java classes

If the base class is not indicated in the declaration of the class with the extends keyword, the default base class is the object class, ex:

public class person{

~~~~~

}

Equivalent to

public class Person extends object{

~~~~~

}

2. The Equals method of the object class

①, the object class is defined as:

public boolean equals (Object obj) method.

Provides logic to define whether objects are equal.

The Equals method of ② and OBJEC is defined as: X.equals (y) returns True when X and Y are applied to the same object, otherwise false.

Some of the classes provided by ③, J2SDK, such as String,date, override the Equals () method of object, invoke the Equals method of these classes, X.equals (y), when X and Y refer to the same class of objects, and the property content is equal (not necessarily an equal object) , returns True otherwise returns false.

④, you can override the Equals () method in the user's custom type as needed.

 Public classtestequals{ Public Static voidMain (String args[]) {Cat CAT1=NewCat (All-in-); Cat Cat2=NewCat (All-in-); System.out.println (CAT1==cat2);                System.out.println (Cat1.equals (CAT2)); String S1=NewString ("Hello"); String S2=NewString ("Hello"); System.out.println (S1==S2);            System.out.println (s1.equals (S2)); }}classcat{intcolor; intHeight,weight; Cat (intColor,intHeight,intweight) {         This. color=color;  This. Height =height;  This. Weight =weight; }         Public Booleanequals (Object obj) {if(obj = =NULL)return false; Else{            if(objinstanceofCat) {Cat C=(Cat) obj; if(C.color = = This. color && C.height = = This. height && C.weight = = This. Weight) {                    return true; }            }        }        return false; }}

Operation Result:

Introduction to the object class in Java

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.