Java object class and its Equals method

Source: Internet
Author: User
Tags function prototype

Tag: private main his/obj this als pre = = Lan

Basic concepts:

    • The object class is located in the Java.lang package, and the Java.lang package contains Java's most basic and core classes, which are automatically imported at compile time;
    • 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. You can use a variable of type object to point to any type of object

Equals () Method: Compare whether two objects are the same

If two objects have the same type and the same property value, the two objects are said to be equal. If two reference objects refer to the same pair of images, the two variables are called the same. The Equals function prototype defined in the object class is:

public boolean equals (object); He is judging whether two objects are the same, not equal

    • ① can only handle reference type variables
    • ② in the object class, finds that Equals () still compares the address values of the two reference variables for equality
 PackageCom.yyx.pratice; Public classTestequals { Public Static voidMain (string[] args) {person P1=NewPerson (); Person P2=NewPerson ();        System.out.println (P1.equals (p2)); System.out.println (P1==p2); /** The String class overrides the Equals () method of the object class to compare whether the entity contents of two objects are identical. */String S1=NewString ("AA"); String S2=NewString ("AA");        System.out.println (s1.equals (S2)); System.out.println (S1==S2); }}classPerson {PrivateString UserName; PrivateString age;  PublicString GetUserName () {returnUserName; }     Public voidsetusername (String userName) { This. UserName =UserName; }     PublicString getage () {returnAge ; }     Public voidsetage (String age) { This. Age =Age ; }}

Java object class and its Equals method

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.