1. Object类

1. 前言

Native 关键字说明其修饰的方法是一个原生态方法,方法的实现不是在当前文件,而是在用其他语言实现的文件中,Java语言本身不能对操作系统底层进行访问和操作,但是可以通过Java Native Interface接口调用其他语言来实现对底层的访问

2. Object主要方法

  1. private static native void registerNatives();
    1. Object类中通过静态代码块的形式保证类加载的时候会调用该方法
  2. public final native Class<?> getClass()
    1. 该方法返回Object的运行时class
  3. public native int hashCode();
    1. 返回一个hash值
    2. 多次请求同一个对象的hashCode方法必须返回同一个int值
    3. 如果两个对象相等(equals方法),那么这两个对象的Hash值相等
    4. 两个对象如果基于equals方法不相同,这两个对象的hasCode方法结果可能相同。注意:两个不相同的对象产生相同的hash code可以提高hash table的效率
  4. public boolean equals(Object obj) {return (this == obj);}
    1. 判断两个对象的引用是否相等
    2. 所有非空对象调用equals(null),返回结果都是false。 null 应该是独立的引用地址
  5. protected native Object clone() throws CloneNotSupportedException;
    1. 深拷贝
    2. x.clone() != x
    3. x.clone().getClass() == x.getClass() 不一定为true
    4. 所有的Arrays都实现了该接口
      1. The method clone for class Object performs a specific cloning operation. First, if the class of this object does not implement the interface Cloneable, then a CloneNotSupportedException is thrown. Note that all arrays are considered to implement the interface Cloneable and that the return type of the clone method of an array type T[] is T[] where T is any reference or primitive type. Otherwise, this method creates a new instance of the class of this object and initializes all its fields with exactly the contents of the corresponding fields of this object, as if by assignment; the contents of the fields are not themselves cloned. Thus, this method performs a "shallow copy" of this object, not a "deep copy" operation.
      2. 上文表示:所有的Arrays都实现了Cloneable接口。在Arrays的实现中,Arrays通过调用clone方法返回并创建类型为T[]新对象。所以新创建的对象是“shallow copy" 而不是原对象的深拷贝。

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

  1. public final native void notify();
    1. 唤醒一个该类中被监控的等待线程
    2. 从等待队列中抽取一个线程被唤醒
    3. 被唤醒的线程不能执行直到现行线程释放锁
  2. public final native void notifyAll();
  3. public final native void wait(long timeout) throws InterruptedException;
  4. protected void finalize() throws Throwable { }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容