Output of System.out.println(object)

后端 未结 4 824
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-29 09:35

I want to know what exactly the output is when I do the following.

class Data {
  int a = 5;
}

class Main {
  public static void main(String[] args) {
    d         


        
4条回答
  •  独厮守ぢ
    2020-11-29 10:23

    When you print an instance of your class, that does not override the toString method, then the toString method of Object class is used. Which prints an output in the form: -

    data@1ae73783
    
    • The first part of that output shows the type of the object.

    • And the 2nd part is the hexadecimal representation of the hashCode of your object.

    Here's the source code of Object.toString() method, that you can find in the installation directory of your jdk, under src folder: -

    public String toString() {
        return getClass().getName() + "@" + Integer.toHexString(hashCode());
    }
    

提交回复
热议问题