今天看到一道面试题object对象包含哪些方法。
刚好没事看看呢看源码。
object对象包含方法大部分都为本地方法【使用C语言写的一些方法】,其中大部分是不可覆盖的;
本地方法中最重要的为
public native int hashCode();
protected native Object clone() throws CloneNotSupportedException;
其他不常用的有
public final native void notify();
public final native void notifyAll();
public final native void wait(long timeout) throws InterruptedException;
public final native Class<? extends Object> getClass();
类方法主要有
public boolean equals(Object obj)
public String toString()
方法就不一一介绍了,但这些方法中最让我注意到的是
public native int hashCode();
public boolean equals(Object obj)
为什么这两个会引起我注意,因为String对象运行的结果与其他对象运行的结果不一致。
对于对象来说equals代表的对象相等【及内存地址的相等】。
从源码中我们也可以看出是对象的比较。
public boolean equals(Object obj) {
return (this == obj);
}
但是我们总会问这样的问题。
String str1= "abc";
String str2= new String( "abc" );
System.out.println( str1.equals( str2 ) );//值为到底为什么。
也行你会说这为两个不同的对象为不等,因为对象的object的equals对象的比较,所以为false。
也行别人也会说告诉你的true,原因就是实验出来的。
到底结果如何,这里我们要注意对与object的equals方法,在子类中可以被覆盖的。
我们再看String的方法发现String对象覆盖object对象的equals方法
public boolean equals(Object anObject) {
//先比较对象是否相等
if (this == anObject) {
return true;
}
//如果为String对象,比较String对象的字符串值
if (anObject instanceof String) {
String anotherString = (String)anObject;
int n = count;
if (n == anotherString.count) {
char v1[] = value;
char v2[] = anotherString.value;
int i = offset;
int j = anotherString.offset;
while (n-- != 0) {
if (v1[i++] != v2[j++])
return false;
}
return true;
}
}
return false;
}
从源码中可以看出String对象的equals方法比较了对象,当对象不相等时再比较字符串的值;
可以看出上述的值为true。
hashcode()方法为获取对象的hash值【及对象的地址值】。
对于非重载的hashCode对象hashCode值一定不同,不一对象hashcode肯定不同
而对于String来说情况就不一样了,先看源码。
public int hashCode() {
int h = hash;
if (h == 0) {
int off = offset;
char val[] = value;
int len = count;
for (int i = 0; i < len; i++) {
h = 31*h + val[off++];
}
hash = h;
}
return h;
}
发现时有字符串是有字符串中的值计算出来的,所以对于两个不同String对象,String字符串值相同时hashCode方法所得到的值是相同。
String str1= new String("abc");
String str2= new String("abc");
System.out.println( str1.hashCode() );
System.out.println( str2.hashCode() );
值都为96354
这里我们也要注意到对象基本类型的包装类如integer,Double,Float等都已经重载了hashcode及equals方法。
这里我列举integer的equals方法
public boolean equals(Object obj) {
if (obj instanceof Integer) {
return value == ((Integer)obj).intValue();
}
return false;
}
这里我们发现它是先判断是否为integer类型然后再做数值比较
那我们看一下示例
Long l = new Long( 9 );
Integer i= new Integer( 9 );
Integer i1= new Integer( 9 );
System.out.println(i.equals( 9 ));
System.out.println(i.equals( l ));
System.out.println(i.equals( i1 ));
System.out.println(i==9);
System.out.println(i==i1);
System.out.println(9==l);
结果会怎么样
从上面可以可以看出结果了
System.out.println(i.equals( 9 ));//true
System.out.println(i.equals( l ));//false l为long类型
System.out.println(i.equals( i1 ));//true
System.out.println(i==9);//true
System.out.println(i==i1);//false i与i1为不同的对象
System.out.println(9==l);//ture
但是注意三个的hashcode是相同的
System.out.println(i.hashCode());//9
System.out.println(i1.hashCode());//9
System.out.println(l.hashCode());//9
其实很多的东西只有看源码就能理解相应的原理及产生原因。所以有机会多看看java的底层源码。