• Post Reply Bookmark Topic Watch Topic
  • New Topic

override toString()  RSS feed

 
Preethi Chilukuri
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Code 1:


RESULT: @hexvalue is printed


code 2:

[Adding code tags - see UseCodeTags for details]

RESULT: Student@hexvalue
Student@hexvalue

What is the common issue in both the scenarios? 1) Trying to append and print String value from an object in a string builder
2) Trying to print String values of an object
I am sure that will solve the issue. I am trying to understand the scenarios to override toString() method.

Please let me know when we will use the override toString() method.

 
Knute Snortum
Sheriff
Posts: 4287
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you should override Object's toString() method most of the time. All classes extend Object implicitly and Object's toString() is what you're seeing.
 
Preethi Chilukuri
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I understand that we have to use toString() when we are printing the string values in the called funtion.

Am I right?
 
Knute Snortum
Sheriff
Posts: 4287
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Basically, yes. System.out.println() calls the toString() method to get the value to print.
 
Campbell Ritchie
Marshal
Posts: 56581
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And you can tell what happens with the append method if you look in the API documentation.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!