• Post Reply Bookmark Topic Watch Topic
  • New Topic

Object class  RSS feed

 
rahul arno
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

why we use toString() method of object class??
its prints out a string message with the name of the class and some numbers???
whats its use?
 
michael ngangom
Greenhorn
Posts: 5
Java MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is basically used to convert or return a readable format of the Object.

toString() needs to be overridden in order to meet your requirement.
 
rahul arno
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
michael ngangom wrote:It is basically used to convert or return a readable format of the Object.

toString() needs to be overridden in order to meet your requirement.

so how can we use the readable format of the Objects can you explain me???
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
rahul arno wrote:so how can we use the readable format of the Objects can you explain me???

Just run it's toString() method. If the class has overridden it, you'll see something that you probably expect; if not you'll see a string like:
someClass@8347a56f
which is what Object.toString() produces.

Winston
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From java.lang.Object#toString JavaDoc

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 is always good to see method's java doc before asking question about the method.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The result of class Object's toString() method is not very useful by itself. You could use it to identify object instances for debugging, for example.

But the designers of class Object had to implement something in Object's toString() method. Suppose that they had decided, for example, that they couldn't put anything meaningful in there, and that they'd made class Object and its toString() method abstract. That would mean that every class would be forced to implement a toString() method. That would have been very inconvenient.
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It would also have been inconvenient not to be able to instantiate Object. There are instances when you want an object, any kind, (any card, as Tommy Cooper used to say), and new Object() fits the bill perfectly.
As for toString(): find a copy of Joshua Bloch’s Effective Java. This link contains a link to a sample chapter of the old edition, which will probably answer your question.
 
Ankush Kaundal
Ranch Hand
Posts: 36
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

See this method is called implicitly whenever you are trying to print the object of the class and so you can override this method and you can provide your own friendly implementation in it.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!