This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Please explain the output of toString() method  RSS feed

 
Singh Kuldeep
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • 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?
 
K. Tsang
Bartender
Posts: 3648
16
Firefox Browser Java Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
thanks K. Tsang!
 
Nikhil Sagar
Ranch Hand
Posts: 216
Java Linux Tomcat Server
  • Mark post as helpful
  • send pies
  • 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)
 
Campbell Ritchie
Marshal
Posts: 55715
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, it is.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!