2

What does java.lang.Object@19821f means ? This is the output when I try to print the variable of Object type without any Assignment. code:

Object object = new Object();
System.out.println(object);
Kumar
  • 31
  • 1
  • It is Object's default `.toString()` implementation. Nothing fancy, as you can see, and which is why you should override `.toString()` in your classes! – fge Jan 29 '13 at 03:35
  • Possible duplicate of [How to support println in a class?](http://stackoverflow.com/questions/27647567/how-to-support-println-in-a-class) – Raedwald Mar 26 '16 at 14:12
  • Possible duplicate of http://stackoverflow.com/questions/29140402/how-do-i-print-my-java-object-without-getting-sometype2f92e0f4 – Raedwald Mar 26 '16 at 14:21

4 Answers4

4

RTFM, Object#toString:

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())

It's important to note that behind the scenes, System.out.println invokes the toString method of its argument.

mre
  • 43,520
  • 33
  • 120
  • 170
0

It's just what the toString() method outputs for the Object type. It's (currently) specified to be:

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

Because Object is the uber-class for all objects, it could be any type of object, so it's expected that derived objects will override the toString() method to provide a more useful representation.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
0

That's the default implementation of Object.toString(). You can even look at the source code for it.

sharakan
  • 6,821
  • 1
  • 34
  • 61
0

This is the hashcode of the Object that you just instantiated. the println method just calls the Objects toString method.

Neil Locketz
  • 4,228
  • 1
  • 23
  • 34