• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Please explain the output of toString() method

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
public class A{}
public class B{
public static void main(String []args){

A a= new A();
B b= new B();
String c= new String("ABS");

System.out.println(a);
System.out.println(b);
System.out.println(c);

}

}

in the above program output is:

A@d2906a
B@1df38fd
ABS


can somebody explain the output. in case of object other than String, why hashcode is printed and why not in case of String?
 
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello there

For your question, when your program do


They are printing a "string representation" of the object. This representation is really calling the object's internal toString() method. Ideally you should override this toString() method as best practice to something more meaningful and human understandable.

The Javadoc for the class java.lang.Object toString() method states:

toString

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

Returns:
a string representation of the object.



This clearly answers your question why it prints out the hashcode.
 
lowercase baba
Posts: 13082
67
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The Object method has a toString() method defined - and it prints the hashcode (basically).

So when you create a new class, you inherit the toString() method.

In the case of the String class, that method has been overridden to do what you would expect. You are free to override the method in your own class as well.
 
Singh Kuldeep
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks K. Tsang!
 
Ranch Hand
Posts: 216
Tomcat Server Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

fred rosenberger wrote:The Object method has a toString() method defined - and it prints the hashcode (basically).

So when you create a new class, you inherit the toString() method.

In the case of the String class, that method has been overridden to do what you would expect. You are free to override the method in your own class as well.



I think It is unsigned hexadecimal representation of a hashcode (basically)
 
Marshal
Posts: 76861
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, it is.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic