[Object-oriented (advanced)] _ object class notes

Source: Internet
Author: User
Tags print object

[Object-oriented (advanced)] _ object class notes

Measure the test taker's knowledge about the functions of object classes.
Measure the test taker's knowledge about the functions of the tostring () and equals () methods in the object class.
Understanding how objects can be used to reference data types

In Java, all classes have a common parent class that is the object class. If a class does not explicitly inherit a class, it must be a subclass of the object class, as shown in the following two rows.CodeThe meaning is the same:
Class person extends ojbect {}
Class person {}

Main methods in the object class:
Method Name type description
Public object constructor
Public Boolean equals (Object OBJ) Common Object comparison
Public int hashcode () Common get hash code
Public String tostring () is called when a common object is printed

Instance 1:

Class demo {}; public class objectdemo01 {public static void main (string ARGs []) {demo d = new demo (); system. out. println ("do not add tostring () Output:" + d); system. out. println ("with tostring () Output:" + D. tostring ());}}

Example 2:

 
Class person {private string name; private int age; Public Person (string name, int age) {This. name = Name; this. age = age;} Public String tostring () {return "name:" + this. name + ", age:" + this. age ;}} public class objectdemo02 {public static void main (string ARGs []) {person per = new person ("Niu Ren", 30); system. out. println ("Object Information:" + per );}}

Print Object Information

Equals () method
The main function of the equals () method is to compare objects.
String itself is also a subclass of the object class, so this method has been overwritten.

Class person {private string name; private int age; Public Person (string name, int age) {This. name = Name; this. age = age;} public Boolean equals (Object OBJ) {If (this = OBJ) {return true;} If (! (OBJ instanceof person) {// not the person object return false;} person per = (person) OBJ; If (Per. name. equals (this. name) & per. age = This. age) {return true;} else {return false;} Public String tostring () {return "name:" + this. name + ", age:" + this. age ;}} public class objectdemo03 {public static void main (string ARGs []) {person Per1 = new person ("Niu Ren", 30 ); person per2 = new person ("Niu Ren", 30); system. out. println (per1.equals (per2 ));}}

Object comparison procedure:
1. Whether to directly equal this = OBJ;
2. Use instanceof to determine whether the instance is a parent class.
3. First, perform a downward transformation and then verify through the object field.

Arrays can also be received using objects.

Public class objectdemo05 {public static void main (string ARGs []) {int temp [] = {1, 3, 5, 7, 9}; object OBJ = temp; print (OBJ );} public static void print (Object OBJ) {If (OBJ instanceof int []) {int X [] = (INT []) OBJ; For (INT I = 0; I <X. length; I ++) {system. out. println (X [I]) ;}}}

Summary:
1. The object class is the parent class of all classes. As long as it is a reference data type, you can use the object to receive
2. The object must undergo an upward transformation before the downward transformation. You must use the instanceof keyword to judge
3. tostring (), called when the object is printed
4. Equals (): called when objects are compared.
5. The string class is also a subclass of the object.

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.