[JAVA basic class library] Objec class

introduction

The Object class is the parent class of all classes and arrays and is located under the java.lang package. That is, Java allows all objects of any type to be assigned to variables of Object type. When a class is defined without explicitly specifying its parent class with the extends keyword, the class inherits the Object parent class by default.
For example:

public class Dog{
......
}
Equivalent to:
public class Dog extends Object {
......
}

Object common methods

The Object class belongs to the java.lang package. All classes under this package do not need to be imported manually. The system will import them automatically during program compilation, so we can use the Object method directly. Here are some common methods;

toString()

Function: returns the descriptive string of the specified object
Generally, the toString method returns a string that "represents" the Object in text. The toString method of the Object class returns a string, which is composed of the class name plus the tag @ and the unsigned hexadecimal representation of the Object hash code. The toString source code of the Object class is as follows:

getClass().getName() + '@' + Integer.toHexString(hashCode())

In fact, it can be understood as outputting the information of the object, which is the method of "self description". When printing and changing the object, the "self description" information of the object will be output to tell the outside world all the state information of the object;
Test code:

package objectlearning;

class Person {
    String name;
    int age;
    // default constructor 
    Person() {}
    // Parametric constructor
    Person(String name, int age) {
        this.name = name;
        this.age = age;
    }
}
public class ToStringTest01 {
    public static void main(String[] args) {
        Person p = new Person();
        System.out.println(p);
    }
}

Output results:

objectlearning.Person@776ec8df

We can see that the return value is "class name + @ + hashCode", which does not really describe the information we want, so we generally need to rewrite the toString class to let the object output the information we need;
Test code:

package objectlearning;

class Person {
    String name;
    int age;
    // default constructor 
    Person() {}
    // Parametric constructor
    Person(String name, int age) {
        this.name = name;
        this.age = age;
    }
    // Override toString method
    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
public class ToStringTest01 {
    public static void main(String[] args) {
        Person p = new Person("Zhang San", 18);
        System.out.println(p);
        // Equivalent to: System.out.println(p.toString());
    }
}

Output results:

Person{name='Zhang San', age=18}

Therefore, toString needs to be rewritten in almost all cases, and the simpler the rewriting, the better. In this way, the obtained object information is brief and clear;
One more thing: the String class has been overridden by SUN

equals()

Compare whether two variables are equal in java. In addition to = =, there is the equals() method;

Let's talk about the usage:

  • For comparison of basic data types, it is recommended to use:==
  • For reference data type comparison (object and object comparison), equals() is recommended and needs to be rewritten!

For the comparison of basic data types, there is no difference between = = and equals. As long as the values of two variables are equal, true will be returned, but = = is simpler and clearer;

For two reference type variables, true will be returned only if they point to the same object; That is, the memory address of the reference variable (the address of the object) is compared;
However, in the actual process, what we want to compare is the content of the object, so we need to override equals();

Rewriting rules: set by yourself. It mainly depends on what is equal to what. When two objects are equal, rewriting must be thorough;

Test code:

package objectlearning;

class Car {
    String color; // colour
    int speed; // speed
    // default constructor 
    public Car() {}
    // Parametric constructor
    public Car(String color, int speed) {
        this.color = color;
        this.speed = speed;
    }

    // Override the equals method
    public boolean equals(Car c) {
        // True if two objects are the same
        if (this == c) return  true;
        // When two objects are different, the color and speed are the same
        if (c != null) return this.color == c.color && this.speed == c.speed;
        return  false;
    }
}
public class EqualsTest01 {
    public static void main(String[] args) {
        Car c1 = new Car("green", 150);
        Car c2 = new Car("blue", 200);
        Car c3 = new Car("green", 150);
        System.out.println("c1 and c2 Are they equal?" + c1.equals(c2));
        System.out.println("c1 and c3 Are they equal?" + c1.equals(c3));
    }
}

Output results:

c1 and c2 Are they equal? false
c1 and c3 Are they equal? true

Similarly, for equals, the String class has been rewritten by SUN, so we don't need to rewrite it again;

summary

Here we briefly introduce the Object class and its two common methods. For Object, remember that it is the father of all classes, and toString and equals need to be rewritten in most cases; If there are other methods in the follow-up, they will be supplemented;

Tags: Java JavaSE object

Posted on Sat, 18 Sep 2021 23:39:59 -0400 by wefollow