java Object class分析

来源:互联网 发布:传奇手游被动技能 数据 编辑:程序博客网 时间:2024/05/28 15:48
Object Class 是所有java类自动继承的父类,看看这个类都有什么成员


private static native voidregisterNatives();
static{
     registerNatives();
}

关联c函数,类似于将c的函数导入过来,这样你就可以调用c的函数了,主要导入的函数是
static JNINativeMethod methods[] = {
{“hashCode”, “()I”, (void *)&JVM_IHashCode},
{“wait”, “(J)V”, (void *)&JVM_MonitorWait},
{“notify”, “()V”, (void *)&JVM_MonitorNotify},
{“notifyAll”, “()V”, (void *)&JVM_MonitorNotifyAll},
{“clone”, “()Ljava/lang/Object;”, (void *)&JVM_Clone},
}; clone
所以当调用这些方法时,实际上是调用的c函数

public native inthashCode();
按照java约定规范,每个object都应该有一个hashcode值,主要用来支持哈希表,可以考虑这个问题,是怎么保证每个对象的哈希值接近不重复,下面英文描述一种方式是将对象的内部地址转化为一个整数作为hash值,还有一种方式通过对字符进行位运算
* As much as is reasonably practical, the hashCode method defined by
* class {@code Object} does return distinct integers for distinct
* objects. (This is typically implemented by converting the internal
* address of the object into an integer, but this implementation
* technique is not required by the
* Java<font size="-2"><sup>TM</sup></font> programming language.)

protected nativeObjectclone()throwsCloneNotSupportedException;
这是一个native方法,按照约定,这个方法是返回一个对象的copy,相当于在内存中把这个对象重新复制了一下,可以研究下copy的深度
copy需要满足的约定:x.clone != x   但x.clone.getClass() == x.getClass() 不是必须的要求。
copy的默认规则,如果是primitive type直接返回,如果是引用类型,返回引用类型,所以注意引用类型
如过一个类支持clone必须实现接口Cloneable,否则抛出CloneNotSupportedException
protected voidfinalize()throwsThrowable { }

java垃圾回收机制规定,在一个对象呗销毁前必须要调用的方法


public final native voidnotify();
public final native voidnotifyAll();
public final native voidwait(longtimeout)throwsInterruptedException;

一些方法主要是和线程有关

总结,Object class 主要是一些native方法,用于jvm和系统减进行交互,还有就是实现java一些基本的约定,比如hashcode,clone这些方法的约定




0 0
原创粉丝点击