當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java Object toString()用法及代碼示例


Java中的每個類都是直接或間接成為Object類的子級。對象類包含toString()方法。我們可以使用toString()方法來獲取對象的字符串表示形式。每當我們嘗試打印對象引用時,就會在內部調用toString()方法。如果我們沒有在您的類中定義toString()方法,則將調用對象類toString()方法,否則將調用我們的已實現/重寫的toString()方法。

對象類toString()方法的語法:

public String toString()
{
      return getClass().getName()+"@"+Integer.toHexString(hashCode());
}
// Java program to illustrate 
// working of toString() method 
class Best_Friend { 
    String name; 
    int age; 
    String college; 
    String course; 
    String address; 
    Best_Friend 
    (String name, int age, String college, String course, String address) 
    { 
        this.name = name; 
        this.age = age; 
        this.college = college; 
        this.course = course; 
        this.address = address; 
    } 
public static void main(String[] args) 
    { 
        Best_Friend b =  
        new Best_Friend("Gulpreet Kaur", 21, "BIT MESRA", "M.TECH", "Kiriburu"); 
        System.out.println(b); 
        System.out.println(b.toString()); 
    } 
}

輸出:


Best_Friend@232204a1
Best_Friend@232204a1

說明:在上麵的程序中,我們創建一個Best_Friend類的對象,並提供朋友的所有信息。但是,當我們嘗試打印對象時,我們會得到一些輸出,輸出形式為classname @ HashCode_in_Hexadeciaml_form。如果我們想要有關Best_friend對象的正確信息,則必須在Best_Friend類中重寫Object類的toString()方法。

// Java program to illustrate 
// working of toString() method 
class Best_Friend { 
    String name; 
    int age; 
    String college; 
    String course; 
    String address; 
    Best_Friend 
    (String name, int age, String college, String course, String address) 
    { 
        this.name = name; 
        this.age = age; 
        this.college = college; 
        this.course = course; 
        this.address = address; 
    } 
public String toString() 
    { 
        return name + " " + age + " " + college + " " + course + " " + address; 
    } 
public static void main(String[] args) 
    { 
        Best_Friend b =  
        new Best_Friend("Gulpreet Kaur", 21, "BIT MESRA", "M.TECH", "Kiriburu"); 
        System.out.println(b); 
        System.out.println(b.toString()); 
    } 
}

輸出:

Gulpreet Kaur 21 BIT MESRA M.TECH Kiriburu
Gulpreet Kaur 21 BIT MESRA M.TECH Kiriburu

注意:在所有包裝器類中,所有集合類,String類,StringBuffer,StringBuilder類toString()方法均被覆蓋以表示有意義的String表示形式。因此,強烈建議在我們的類中也重寫toString()方法。

// Java program to illustrate 
// working of toString() method 
import java.util.*; 
class Best_Friend { 
    String name; 
    int age; 
    String college; 
    String course; 
    String address; 
    Best_Friend 
    (String name, int age, String college, String course, String address) 
    { 
        this.name = name; 
        this.age = age; 
        this.college = college; 
        this.course = course; 
        this.address = address; 
    } 
public static void main(String[] args) 
    { 
        Best_Friend b =  
        new Best_Friend("Gulpreet Kaur", 21, "BIT MESRA", "M.TECH", "Kiriburu"); 
        System.out.println(b); 
        String s = new String("Gulpreet Kaur"); 
        System.out.println(s); 
        Integer i = new Integer(21); 
        System.out.println(i); 
        ArrayList l = new ArrayList(); 
        l.add("BIT"); 
        l.add("M.TECH"); 
        System.out.println(l); 
    } 
}

輸出:

Best_Friend@232204a1
Gulpreet Kaur
21
[BIT, M.TECH]


相關用法


注:本文由純淨天空篩選整理自 Object toString() Method in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。