• Post Reply Bookmark Topic Watch Topic
  • New Topic

what does this output means in Java  RSS feed

 
bernard kissi
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Trying to print an output in the following code below


and i get this output
drinksController@74c7e8d7

can i know please know the meaning of this output?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bernard kissi wrote:
and i get this output
drinksController@74c7e8d7

can i know please know the meaning of this output?


When you print an object, the library will call the toString() method to convert the object to a string first.  The reason that it is done this way, and probably not the way you want it, is because the Java library can't read your mind, and know what you mean by printing an object...

And... if you don't override the toString() method, you will use the implementation from the Object class. This implementation simply prints the types, along with its identity hash.

Henry
 
bernard kissi
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so in this case i have to override the toString method to get the desire output if i got you right
 
Campbell Ritchie
Marshal
Posts: 56593
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. Override toString.
 
salvin francis
Bartender
Posts: 1664
37
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Overriding toString may achieve the output you want, but is not necessarily the best thing.

In my opinion, toString() should only be used for debugging purposes. If you are printing a message to show the end user and your message is depending on an object's toString method, it would smell bad to me. A good implementation of toString() would be to show the current "state" an object is in alongside most of the relevant fields.

As long as you want to only use it for debugging for a programmer, it should be fine. I would also suggest using a logging framework instead of System.out.print method.
 
Campbell Ritchie
Marshal
Posts: 56593
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not convinced that toString is intended only for debugging. You can create a simple method like this:-That can be used for both.

By the way: the names of the methods are incorrect: they shou‍ld not contain _ underscores. Java® identifiers shou‍ld be written in MixedCase or mixedCaseStartingLowerCase.
 
salvin francis
Bartender
Posts: 1664
37
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Not convinced that toString is intended only for debugging...

My preference would be to construct end user readable info using MessageFormat instead.

Object structures and field variables can change quite often, more fields can be added. The user experience should not be depending on an object's toString() in my opinion. A typical example in the above drinks controller case is someone adding a "costToCompany"/"profitPerDrink" field and including it in toString()

I would not like toString to be used for either Business Logic or end user facing messages.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!