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