HashCode();wait();notify();equals();getClass();toString();clone();finalize();java
這裏只是簡單介紹一下其中的幾個函數:函數
HashCode():學習
* 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™ programming language.)
這個是Java官方文檔裏的部分解釋,簡單地說就是返回一個integer類型的值,這個值是經過該Object的內部地址(internal address)轉換過來的,這個哈希碼是能夠經過getClass()方法看到具體值的,顯示的是十六進制的數,有時候能夠經過此方法來判斷對象的引用是否相等,研究java內存的時候,這個可能會有用。ui
equals():this
都知道java中比較字符串是否相等應該用equals();而==則是比較的值,也就是引用;那麼爲何是這樣的呢?我今天看了下equals的實現方法,發現字符串的比較,是經過每個字符進行比較,若是都相等,則返回true;而源碼裏面Object類型的equals方法倒是直接用==來代替的,也就是說至關於直接比較引用了,這也就是爲何咱們有時候要重寫equals方法了,下面附上Jdk中String類型的equals方法源碼。spa
public boolean equals(Object anObject) { if (this == anObject) { return true; } if (anObject instanceof String) { String anotherString = (String)anObject; int n = value.length; if (n == anotherString.value.length) { char v1[] = value; char v2[] = anotherString.value; int i = 0; while (n-- != 0) { if (v1[i] != v2[i]) return false; i++; } return true; } } return false; }
getClass(): .net
toString():線程
下面的例子中j是一個簡單的Java對象,後面分別是toString()方法和getClass()方法的輸出,@後面的即爲哈希碼,也就是內存地址,getClass()返回運行時的類,code
還有getName(),getSimpleName()方法,這些均可以經過查看源碼的方法來了解用法,看源碼真的是很好的一種學習方法。對象
System.out.println(j.toString()); System.out.println(j.getClass()); com.wust.cvte.j2se@15db9742 class com.wust.cvte.j2se
clone():
中文」克隆"的意思,剛開始我看源碼裏的註釋,看的並非很懂,後來又在網上查看一些博客,明白了clone方法其實是實現了淺複製,附上一篇詳解此方法的博客,有圖有真相,特別詳細,http://blog.csdn.net/zhangjg_blog/article/details/18369201#0-qzone-1-28144-d020d2d2a4e8d1a374a433f596ad1440
其它方法涉及到線程和內存回收,GG等,到用的時候再作記錄吧,其中如有不正確之處,歡迎指證。