16. Object类

16. Object类

Java类不是单继承么?如果一个类继承了其他父类,就不会直接继承Object,但是它的父类如果再没有父类,就会继承Object,也就是说,Object是任何一个类的直接或间接的父类。因此,我们随意创建一个类,其实也不是空的,起码这个类中有Object类中的属性和方法。

例如:

public class Example {

    public Example() {

    }

}
public class Client {

    public static void main(String[] args) {

        //任何类都是Object的派生类或者子类
        Example example = new Example();

        System.out.println(example.equals(null));
        System.out.println(example.getClass());
        System.out.println(example.hashCode());
        System.out.println(example.toString());

    }

}

输出:

false
class com.company.project.demo0.Example
2018699554
com.company.project.demo0.Example@7852e922

1、Object类的作用和地位

Object类在java.lang包下,是类层次结构的根类。每个类都使用 Object 作为超类。所有对象(包括数组)都实现了这个类的方法。

例如:

public class Example {

    public static void main(String[] args) {

        //数组也是Object的子类
        int[] array = new int[0];

        System.out.println(array.equals(null));
        System.out.println(array.getClass());
        System.out.println(array.hashCode());
        System.out.println(array.toString());

    }

}

输出:

false
class [I
2018699554
[I@7852e922

2、Object类的方法

protected Object clone() Creates and returns a copy of this object. 创建并返回此对象的一个副本。

boolean equals(Object obj) Indicates whether some other object is “equal to” this one. 判断其他某个对象是否与此对象“相等”。

protected void finalize() Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.A subclass overrides the finalize method to dispose of system resources or to perform other cleanup. 当垃圾回收器确定不存在对该对象的更多引用时,由对象的垃圾回收器调用此方法。 一个子类覆盖finalize方法来处理系统资源或执行其他清理。

Class<?> getClass() Returns the runtime class of this Object. 返回此 Object 的运行时类。

int hashCode() Returns a hash code value for the object. 返回该对象的哈希码值。

void notify() Wakes up a single thread that is waiting on this object’s monitor. 唤醒在此对象监视器上等待的单个线程。

void notifyAll() Wakes up all threads that are waiting on this object’s monitor. 唤醒在此对象监视器上等待的所有线程。

String toString() Returns a string representation of the object. 返回该对象的字符串表示。

void wait() Causes the current thread to wait until another thread invokes the notify() method or the notifyAll() method for this object.In other words, this method behaves exactly as if it simply performs the call wait(0). 导致当前线程等待,直到另一个线程调用此对象的notify()方法或notifyAll()方法。换句话说,这个方法的行为与它简单地执行调用wait(0)完全一样。

void wait(long timeout) Causes the current thread to wait until either another thread invokes the notify() method or the notifyAll() method for this object, or a specified amount of time has elapsed. 导致当前线程等待,直到另一个线程调用此对象的notify()方法或notifyAll()方法,或者经过指定的时间量。

void wait(long timeout, int nanos) Causes the current thread to wait until another thread invokes the notify() method or the notifyAll() method for this object, or some other thread interrupts the current thread, or a certain amount of real time has elapsed. 导致当前线程等待,直到另一个线程调用此对象的notify()方法或notifyAll()方法,或其他一些线程中断当前线程,或已经过了一定时间。

3.Object常用方法

3.1 toString方法

toString方法可以返回该对象的字符串表示。或者说将任何一个对象转换成字符串返回。方法定义如下:

public String toString() {
        return getClass().getName() + "@" + Integer.toHexString(hashCode());
    }

通常,toString 方法会返回一个“以文本方式表示”此对象的字符串。结果应是一个简明但易于读懂的信息表达式。建议所有子类都重写此方法。 Object 类的 toString 方法返回一个字符串,该字符串由类名(对象是该类的一个实例)、at 标记符“@”和此对象哈希码的无符号十六进制表示组成。换句话说,该方法返回一个字符串,它的值等于: getClass().getName() + ‘@’ + Integer.toHexString(hashCode())

3.2 equals方法和==

3.2.1、equals方法

equals方法判断其它某个对象是否与此对象“相等”。方法定义如下:

public boolean equals(Object obj) {
        return (this == obj);
    }

​ 由方法定义可知,Object类中的equals方法,用来比较两个引用的虚地址。当且仅当两个引用在物理上是同一个对象时,返回值为true,否则将返回false。

equals 方法在非空对象引用上实现相等关系:

●自反性:对于任何非空引用值 x,x.equals(x) 都应返回 true。
●对称性:对于任何非空引用值 x 和 y,当且仅当 y.equals(x) 返回 true 时,x.equals(y) 才应返回 true。
●传递性:对于任何非空引用值 x、y 和 z,如果 x.equals(y) 返回 true,并且 y.equals(z) 返回 true,那么 x.equals(z) 应返回 true。
●一致性:对于任何非空引用值 x 和 y,多次调用 x.equals(y) 始终返回 true 或始终返回 false,前提是对象上 equals 比较中所用的信息没有被修改。
●对于任何非空引用值 x,x.equals(null) 都应返回 false。 Object 类的 equals 方法实现对象上差别可能性最大的相等关系;即,对于任何非空引用值 x 和 y,当且仅当 x 和 y 引用同一个对象时,此方法才返回 true(x == y 具有值 true)。

**注意:**当此方法被重写时,通常有必要重写 hashCode 方法,以维护 hashCode 方法的常规协定,该协定声明相等对象必须具有相等的哈希码。

3.2.2、==

==比较的是内存中的虚地址。

具体equals方法和==的区别和原理,我会另做一篇文章。

3.3、hashCode方法

3.3.1、hashCode方法

hashCode方法用来获取对象的哈希码值。支持此方法是为了提高哈希表(例如 java.util.Hashtable 提供的哈希表)的性能。方法定义如下:

public native int hashCode();

hashCode的常规协定是: 在 Java 应用程序执行期间,在对同一对象多次调用 hashCode 方法时,必须一致地返回相同的整数,前提是将对象进行 equals 比较时所用的信息没有被修改。从某一应用程序的一次执行到同一应用程序的另一次执行,该整数无需保持一致。 如果根据 equals(Object) 方法,两个对象是相等的,那么对这两个对象中的每个对象调用 hashCode 方法都必须生成相同的整数结果。 如果根据 equals(java.lang.Object) 方法,两个对象不相等,那么对这两个对象中的任一对象上调用hashCode方法不要求一定生成不同的整数结果。但是,程序员应该意识到,为不相等的对象生成不同整数结果可以提高哈希表的性能。 实际上,由 Object 类定义的 hashCode 方法确实会针对不同的对象返回不同的整数。(这一般是通过将该对象的内部地址转换成一个整数来实现的,但是 Java编程语言不需要这种实现技巧。)

3.3.2、equals方法与hashCode方法关系

-如果两个对象使用equals比较返回true,那么它们的hashCode值一定要相同。 -如果两个对象equals比较返回false,那么它们的hashCode值不一定不同。 -覆盖equals,往往需要覆盖hashCode,可以使用Eclipse自动生成,保证equals返回true,则hashCode相同;equals返回false,则hashCode不同。 -在Set集合部分有实际应用。

4、Object和Object[]之间的区别

方法中的形参是Object类型时,任何引用类型的参数都可以传进去执行。 方法中形参是Object[]类型时,只有对象数组可以传入执行。

部分内容来自老师阿宝哥的笔记~~~

作者:AT阿宝哥 链接:https://www.jianshu.com/p/98638a3a399e 来源:简书

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值