6

This program

public class HelloWorld{
    public void testFunc(){
        System.out.println("Class = "+this);
    }

    public static void main(String[] args){
        HelloWorld hw = new HelloWorld();
        System.out.println("Hello, World");
        hw.testFunc();
    }
}  

gives me this output:

Hello, World
Class = HelloWorld@7c6768

What does @7c6768 after HelloWorld in the second line mean?

user13267
  • 6,871
  • 28
  • 80
  • 138

9 Answers9

13

Object's toString() is implemented as follows:

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

Since your HelloWorld class doesn't override it, this is the method called.

jlordo
  • 37,490
  • 6
  • 58
  • 83
  • I'm sorry but I want to ask a very basic question that I just seem to understand: In your code when you say `getClass().getName()`, which class do getClass() and getName() belong to? Is getClass()a member function of the class in which this toString() function is defined? If so then where does getName() come from? – user13267 Jul 26 '13 at 10:09
  • Every class in Java extends `Object`, see http://docs.oracle.com/javase/7/docs/api/java/lang/Object.html That's why every class also has the `getClass()` method. – jlordo Jul 26 '13 at 10:11
9

The toString() method returns a string representation of the object.

In general, the toString() method returns a string that "textually represents" this object. The result should be a concise but informative representation that is easy for a person to read. It is recommended that all subclasses override this method.

The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@', and the unsigned hexadecimal representation of the hash code of the object. In other words, this method returns a string equal to the value of:

 getClass().getName() + '@' + Integer.toHexString(hashCode())
Radu Murzea
  • 10,724
  • 10
  • 47
  • 69
MayurB
  • 3,609
  • 2
  • 21
  • 36
7

As per Docs of toString() method in Object class

The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@', and the unsigned hexadecimal representation of the hash code of the object. In other words, this method returns a string equal to the value of:

When

 getClass().getName() + '@' + Integer.toHexString(hashCode())

When you call toString() on object ,If you ovveride like below ,you get your own implementation

 @Override
  public String toString() {
     //return something 
  }

Otherwise gives the default implementation,which you are seeing right now

From Object class Source code

Returns a string representation of the object. In general, the toString method returns a string that "textually represents" this object. The result should be a concise but informative representation that is easy for a person to read. It is recommended that all subclasses override this method.

The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@', and the unsigned hexadecimal representation of the hash code of the object. In other words, this method returns a string equal to the value of: getClass().getName() + '@' + Integer.toHexString(hashCode())

Returns:
a string representation of the object.


    public String  toString() {
         return getClass().getName() + "@" + Integer.toHexString(hashCode());
     }
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
  • 1
    I just wanted to add: you can override the toString() method in that object in order to print something more meaningful. – Lenymm Jul 26 '13 at 10:04
  • Yeah if I define a function as `@Override public String toString(){ return "TEST"; } ` it shows me TEST instead of the default value. – user13267 Jul 26 '13 at 10:12
3

From the API:

The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@', and the unsigned hexadecimal representation of the hash code of the object

morgano
  • 17,210
  • 10
  • 45
  • 56
3

HelloWorld@7c6768 is a string representation of the current object, and @7c6768 is a hashcode. In fact you are invoking toString() of current object

Here is java doc for toString() http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/Object.html#toString()

ThiepLV
  • 1,219
  • 3
  • 10
  • 21
3

If you see the toString() method in Object class

/**
 * Returns a string representation of the object. In general, the
 * {@code toString} method returns a string that
 * "textually represents" this object. The result should
 * be a concise but informative representation that is easy for a
 * person to read.
 * It is recommended that all subclasses override this method.
 * <p>
 * The {@code toString} method for class {@code Object}
 * returns a string consisting of the name of the class of which the
 * object is an instance, the at-sign character `{@code @}', and
 * the unsigned hexadecimal representation of the hash code of the
 * object. In other words, this method returns a string equal to the
 * value of:
 * <blockquote>
 * <pre>
 * getClass().getName() + '@' + Integer.toHexString(hashCode())
 * </pre></blockquote>
 *
 * @return  a string representation of the object.
 */
public String toString() {
    return getClass().getName() + "@" + Integer.toHexString(hashCode());
}

It returns Class name followed by its hash code. That is the number you are getting.

Aniket Thakur
  • 66,731
  • 38
  • 279
  • 289
2

That is this.hashCode(). Since you do not redefine hashCode(), this number is the memory address in the JVM where the object is stored.

tbodt
  • 16,609
  • 6
  • 58
  • 83
2

Look inside Objects' toString() method:

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

It is a hash value of the object.

Alexey Odintsov
  • 1,705
  • 11
  • 13
2

The number that identifies the object uniquely. It is Hexadecimal representation of hashcode. In simple terms, the whole String printed is the reference returned after instantiating the class.

Code Enthusiastic
  • 2,827
  • 5
  • 25
  • 39