[javaSE] 物件導向(Object類toString),javasetostring

來源:互聯網
上載者:User

[javaSE] 物件導向(Object類toString),javasetostring

每一個對象,都有一個在記憶體中的地址雜湊值,這個雜湊值是十六進位的

 

調用Object對象的hashCode()方法,返回這個對象的雜湊值

調用Integer.toHexString()方法,轉換十六進位

調用Object對象的toString()方法,得到:類名@雜湊值

通常我們會複寫toString()方法,因為預設的沒有太大意義

 

實現原理是類的反射

當我們建立一個對象,會在硬碟上產生xxx.class的檔案,jdk定義了Class類來描述這些class檔案

調用Object對象的getClass()方法,得到Class對象

調用Class對象的getName()方法,得到類的名稱

public class ObjectDemo {    /**     * @param args     */    public static void main(String[] args) {        ObjectDemo od=new ObjectDemo();        System.out.println(od.toString());//輸出 ObjectDemo@26f04d94        System.out.println(Integer.toHexString(od.hashCode()));//輸出 26f04d94                //反射        Class c=od.getClass();        System.out.println(c.getName()+"@"+Integer.toHexString(od.hashCode()));    }}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.