Java's Object class

A, Object Overview

    java.lang.Object class is the root class in the Java language, that is the parent of all classes.

  In the object instantiation when the final look of the parent class is Object.

  If a class is not specified parent class, then the default is inherited from the Object class.

  Demo:

1 public class MyClass /*extends Object*/ {
2       // ...
3 }

  Object class which contains methods 11, the following mainly describes two of them:

public String toString () `: Returns a string representation of the object. 
public boolean equals (Object obj): indicate whether some other object with this object is "equal."

Two, toString method

  method:

  public String toString(): Returns a string representation of the object.

  toString method returns a string representation of the object, in fact, is the type of content that the string @ + + memory address value of the object.

  As a result toString method returns the memory address, and in development, the need to give the corresponding string representation according to properties of the object, and therefore need to be rewritten.

   Overwrite:

  If you do not want to use the default behavior toString, then it can be overwritten.

  Demo:

 1 public class Person {  
 2     private String name;
 3     private int age;
 4 
 5     @Override
 6     public String toString() {
 7         return "Person{" + "name='" + name + '\'' + ", age=" + age + '}';
 8     }
 9 
10     // 省略构造器与Getter Setter
11 }

 

  Tips : When we use the direct output statements output object name, in fact, by the object calling its toString () method.

Three, equals method

Four, Objects method

Guess you like

Origin www.cnblogs.com/niujifei/p/11409712.html