文章目录

一、定义

Object是所有类的父类,任何类都默认继承Object。

/**
* Class {@code Object} is the root of the class hierarchy.
* Every class has {@code Object} as a superclass. All objects,
* including arrays, implement the methods of this class.
*/

二、构造方法

源码中并没有Object的构造方法,但是,同样的,编译器在编译期间会给Object(事实上,所有的Java类,只要类中没有构造方法,编译器都会默认的给一个空构造方法,若已有构造方法,则不会添加)一个默认的空的构造方法。

三、成员方法

1、toString

源码中的toString方法输出类名及哈希值

/**
* Returns a string representation of the object. In general, the
* {@code toString} method returns a string that
* "textually represents" this object. The result should
* be a concise but informative representation that is easy for a
* person to read.
* It is recommended that all subclasses override this method.
* <p>
* The {@code toString} method for class {@code Object}
* returns a string consisting of the name of the class of which the
* object is an instance, the at-sign character `{@code @}', and
* the unsigned hexadecimal representation of the hash code of the
* object. In other words, this method returns a string equal to the
* value of:
* <blockquote>
* <pre>
* getClass().getName() + '@' + Integer.toHexString(hashCode())
* </pre></blockquote>
*
* @return a string representation of the object.
*/
public String toString() {
return getClass().getName() + "@" + Integer.toHexString(hashCode());
}

该方法在子类中一般推荐进行重写,以下我们命名一个Person类重写该方法,并测试重写后的输出结果。

public class Person extends Object {

private String name;

private String sex;

private int age;

//重写toString方法
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", sex='" + sex + '\'' +
", age=" + age +
'}';
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getSex() {
return sex;
}

public void setSex(String sex) {
this.sex = sex;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}
}

public class PersonTest {

public static void main(String[] args) {
Person person = new Person();
person.setName("小明");
person.setSex("男");
person.setAge(16);
System.out.println(person.toString());
}
}

输出结果如下:

Java Object类_构造方法

2、equals

默认的equals方法,默认比较的是两个对象的地址值。

/**
* Indicates whether some other object is "equal to" this one.
* <p>
* The {@code equals} method implements an equivalence relation
* on non-null object references:
* <ul>
* <li>It is <i>reflexive</i>: for any non-null reference value
* {@code x}, {@code x.equals(x)} should return
* {@code true}.
* <li>It is <i>symmetric</i>: for any non-null reference values
* {@code x} and {@code y}, {@code x.equals(y)}
* should return {@code true} if and only if
* {@code y.equals(x)} returns {@code true}.
* <li>It is <i>transitive</i>: for any non-null reference values
* {@code x}, {@code y}, and {@code z}, if
* {@code x.equals(y)} returns {@code true} and
* {@code y.equals(z)} returns {@code true}, then
* {@code x.equals(z)} should return {@code true}.
* <li>It is <i>consistent</i>: for any non-null reference values
* {@code x} and {@code y}, multiple invocations of
* {@code x.equals(y)} consistently return {@code true}
* or consistently return {@code false}, provided no
* information used in {@code equals} comparisons on the
* objects is modified.
* <li>For any non-null reference value {@code x},
* {@code x.equals(null)} should return {@code false}.
* </ul>
* <p>
* The {@code equals} method for class {@code Object} implements
* the most discriminating possible equivalence relation on objects;
* that is, for any non-null reference values {@code x} and
* {@code y}, this method returns {@code true} if and only
* if {@code x} and {@code y} refer to the same object
* ({@code x == y} has the value {@code true}).
* <p>
* Note that it is generally necessary to override the {@code hashCode}
* method whenever this method is overridden, so as to maintain the
* general contract for the {@code hashCode} method, which states
* that equal objects must have equal hash codes.
*
* @param obj the reference object with which to compare.
* @return {@code true} if this object is the same as the obj
* argument; {@code false} otherwise.
* @see #hashCode()
* @see java.util.HashMap
*/
public boolean equals(Object obj) {
return (this == obj);
}

一般推荐重写equals方法,比如我们要比较Person对象的姓名,年龄等;

//重写equals方法
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Person person = (Person) o;
return age == person.age &&
Objects.equals(name, person.name) &&
Objects.equals(sex, person.sex);
}

public class PersonTest {
public static void main(String[] args) {
Person person1 = new Person();
person1.setName("小明");
person1.setSex("男");
person1.setAge(16);
System.out.println(person1.toString());

Person person2 = new Person();
person2.setName("小明");
person2.setSex("男");
person2.setAge(16);
System.out.println(person2.toString());

System.out.println(person1.equals(person2));

}
}

测试结果如下;

Java Object类_成员方法_02

3、hashCode

同上我们也可以重写hashCode方法

//重写hashCode
@Override
public int hashCode() {
return Objects.hash(name, sex, age);
}

四、Objects类

Objects类是final修饰的类,不可继承,内部方法都是static方法,从jdk1.7开始才引入了Objects类。

1、Objects类的equals静态方法
/**
* Returns {@code true} if the arguments are equal to each other
* and {@code false} otherwise.
* Consequently, if both arguments are {@code null}, {@code true}
* is returned and if exactly one argument is {@code null}, {@code
* false} is returned. Otherwise, equality is determined by using
* the {@link Object#equals equals} method of the first
* argument.
*
* @param a an object
* @param b an object to be compared with {@code a} for equality
* @return {@code true} if the arguments are equal to each other
* and {@code false} otherwise
* @see Object#equals(Object)
*/
public static boolean equals(Object a, Object b) {
return (a == b) || (a != null && a.equals(b));
}

根据以上源码我们可以在测试代码中使用Objects类的equals方法进行两个Person对象的比较。

public class PersonTest {

public static void main(String[] args) {
Person person1 = new Person();
person1.setName("小明");
person1.setSex("男");
person1.setAge(16);
System.out.println(person1.toString());

Person person2 = new Person();
person2.setName("小明");
person2.setSex("男");
person2.setAge(16);
System.out.println(person2.toString());

System.out.println(Objects.equals(person1, person2));

}
}

测试结果如下:

Java Object类_成员方法_03