• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

toString method in Object Class

 
Greenhorn
Posts: 16
Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
why is the toString method kept in Object class ?
though it is a method to return the Object in form of a String
why not kept in any of 4 classes of String
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What are the 4 classes of String you referring to? All the while I was thinking that String was only one class?
http://docs.oracle.com/javase/6/docs/api/java/lang/String.html
Anyways, you have a valid question!
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's the question you should answer: why should it not be on Object?
 
Ranch Hand
Posts: 228
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

pratik gaurav wrote:why is the toString method kept in Object class ?
though it is a method to return the Object in form of a String
why not kept in any of 4 classes of String



If it was only for String class then how would we get some meaningful name for our custom made classes object and other Java Objects other than String.
Getting??

say:





If toString was not defined in the Object class then we would always get some hashcode values.

Hope it some what clears.
 
Ranch Hand
Posts: 233
1
Eclipse IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

pratik gaurav wrote:why is the toString method kept in Object class ?
though it is a method to return the Object in form of a String
why not kept in any of 4 classes of String


Hi Pratik,
Figure out answer to Bear Bibeault's question

Here's the question you should answer: why should it not be on Object?


Or turning around,
1. why do we have the Object class and its constituents?
2. How have you used Object class so far?

Please answer.
Thanks
 
Abdulla S Mamuwala
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ishan, I dont totally agree with what you have mentioned.

If it was only for String class then how would we get some meaningful name for our custom made classes object and other Java Objects other than String.
Getting??


If toString was not defined in the Object class then we would always get some hashcode values.



I don't think it is necessary to use toString() from Object class to get meaningful information about your object.

For example,



As you can see toSting() would infact give bad info. I am sure you meant to give a different explanation?
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Abdulla S Mamuwala wrote:
I don't think it is necessary to use toString() from Object class to get meaningful information about your object.

For example,


As you can see toSting() would infact give bad info. I am sure you meant to give a different explanation?



You've missed the point. With your approach, we have to know that Car has description(), Animal has whatAmI(), and Person has generateSomeTextThatDescribesThisPersonObject(). We wouldn't be able to do System.out.println(someCar) and get a meaningful output. When using a logging framework like log4j, we'd have to know what the object is, call the appropriate method for that class, and pass the resulting String, rather than just passing the object and letting the logger handle it.

That's the main purpose of inheritance, and that's why Object eixsts as the root of Java's class hierarchy: So that we can say "This thing is of type X (Object, in this example) and every X can do these operations (toString(), equals(), hashCode(), getClass(), etc.)"

As you can see toSting() would infact give bad info.



Only if you don't understand it and don't properly override it.
 
pratik gaurav
Greenhorn
Posts: 16
Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Abdulla S Mamuwala wrote:What are the 4 classes of String you referring to? All the while I was thinking that String was only one class?
http://docs.oracle.com/javase/6/docs/api/java/lang/String.html
Anyways, you have a valid question!



the 4 classes are
1. java.lang.String
2. java.lang.StringBuffer
3. java.lang.StringBuilder
4. java.lang.StringTokenizer
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

pratik gaurav wrote: . . .
the 4 classes are
1. java.lang.String
2. java.lang.StringBuffer
3. java.lang.StringBuilder
4. java.lang.StringTokenizer

No, no, no, no, no.

StringTokenizer is not a String at all. It does something with a String, but is not a String. Anyway, you ought not to use it any more.
You should no longer use StringBuffer, either. It is very similar to StringBuilder, which is designed for making Strings rapidly.
 
Abdulla S Mamuwala
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jeff, looks like you have missed the point I was making to Ishan. I am by no means proposing to use description() method instead of toString(), I am just saying that there is another alternative we can use instead of toString(), although it might not be the best objective oriented practice.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Abdulla S Mamuwala wrote:Hi Jeff, looks like you have missed the point I was making to Ishan. I am by no means proposing to use description() method instead of toString(), I am just saying that there is another alternative we can use instead of toString(), although it might not be the best objective oriented practice.



But that alternative has nothing to do with the question at hand. Sure, we can create our own version of any method in any class that does the same thing as another method. So what? The question at hand in this thread is "Why is toString() defined on Object?" and that is what I was addressing. If your point was simply that "We can make any old method we want on our classes that returns a String that describes the object," well, yes, that's true, but it's not particularly relevant or useful.

I also assumed the question "how would we get some meaningful name for our custom made classes object and other Java Objects other than String." was also in the context of what this thread is about: Namely, "Without some method that we know is present in every class, how would we be able to get a meaningful description of an arbitrary object?" That's the question I thought you were answering, as it relates to the topic of the thread.
 
Abdulla S Mamuwala
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi pratik, thanks for the clarification. Normally, the 4 classes you pointed out are not in technical jargon referred to as "4 classes of String ", that would potentially imply something else, or may be nothing at all:)
 
Abdulla S Mamuwala
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi jeff, sorry but I would disagree,


"We can make any old method we want on our classes that returns a String that describes the object," well, yes, that's true, but it's not particularly relevant or useful.


May be in one scenario it is not relevant but in others it might be?
say my client wants to use the Car class I built and wants to know the description of the car from the Car object he builds. He prefers to know this description by calling a method named "description" which is more english like and more intuitive then "toString()". Then should I force him to use toString() because it is more object oriented? So the way to know about an object is not only by using toString() but there are alternative approaches that you can use.
Please have a look at what I mentioned in the post to Ishan, I think you misunderstood it. Even before I give an example this what I say,


I don't think it is necessary to use toString() from Object class to get meaningful information about your object.


I was replying to Ishan's answer,


If toString was not defined in the Object class then we would always get some hashcode values.


 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Abdulla S Mamuwala wrote:Hi jeff, sorry but I would disagree,


"We can make any old method we want on our classes that returns a String that describes the object," well, yes, that's true, but it's not particularly relevant or useful.


May be in one scenario it is not relevant but in others it might be?



It's not relevant in the context of this thread.

say my client wants to use the Car class I built and wants to know the description of the car from the Car object he builds. He prefers to know this description by calling a method named "description" which is more english like and more intuitive then "toString()". Then should I force him to use toString()



You are still missing the point.

1. A programmer will not be put off by toString() vs. description(). We're not creating libraries for non-programmers here.

2. You can still add description(), and you can have it call toString(), or have toString() call it, or have them return two completely different things. I've done all three in my time.

3. The point, in relation to the topic of this thread, is (and this has been stated a couple of times): We want to have a method that we can call on any object, without knowing what kind of object it is or what special methods it may have added for itself. I'm not saying we can't add other specialized methods to our classes. What I AM saying is that those methods are not a replacement for toString().
 
Abdulla S Mamuwala
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
again, you misunderstood


A programmer will not be put off by toString() vs. description(). We're not creating libraries for non-programmers here.


Did I mention anything about programmers vs non-programmers??


The point, in relation to the topic of this thread, is (and this has been stated a couple of times): We want to have a method that we can call on any object, without knowing what kind of object it is or what special methods it may have added for itself. I'm not saying we can't add other specialized methods to our classes. What I AM saying is that those methods are not a replacement for toString().


Well if you are not going to use toString(), then of course they are a replacement for toString().

Anyways, nitpicking is not going to help anyone. As long as pratik, gets the point we are good!
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Abdulla S Mamuwala wrote:Anyways, nitpicking is not going to help anyone.



Actually, nitpicking can help people truly understand something and use it correctly as opposed to just having a vague idea. And when communicating with a computer (i.e., programming), it is important to state very precisely what you want it to do. If you are not exact, then in the best case your code will not compile, in the next best case it will crash when you run it, and in the worst case it will silently chug along, doing the wrong thing without you realizing until it's too late.

As for the rest, I'm willing to let it drop as long as the OP understands why it's valuable to have toString() defined on Object.
 
Abdulla S Mamuwala
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Truly, I thought I was communicating with a human and not a computer!

dictionary
Definitions on nitpicking,
verb (used without object)
1. to be excessively concerned with or critical of inconsequential details.
verb (used with object)
2. to criticize by focusing on inconsequential details.
noun
3. a carping, petty criticism.
adjective
4. of, pertaining to, or characteristic of a nitpicker or nitpicking.

I don't see anything positive in nitpicking. Oh! yes but you are talking about "computers"!
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

pratik gaurav wrote:why is the toString method kept in Object class ?
though it is a method to return the Object in form of a String
why not kept in any of 4 classes of String


Simply put: because the designers of Java thought it likely that every object might need a String representation; and if you look carefully at the docs, you'll see that the String it returns is meant mainly for debugging, NOT for displaying to the public (although because the two are often the same, many lazy designers have simply forgotten this).

On that point I agree with Abdulla: if you intend the String to be used for something specific, then define a specific method; and if that requirement covers more than one class, define it in an interface and have classes that need it implement it.

Winston
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Abdulla S Mamuwala wrote:Truly, I thought I was communicating with a human and not a computer!


You are. You're simply running into the fact that Jeff is one of the best I know at picking out inconsistencies in argument (I know, because I've been on the receiving end more than once ).

If you read my previous post, you'll see that I basically agree with some of what I think you were trying to say; Jeff's just pointing out the inconsistencies in your post because....well, that's what Jeffs do.

And one of these days, you'll thank him.

Winston
 
Abdulla S Mamuwala
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Winston, thanks for giving your 2 cents. Looking back at the post I think Bear's answer was the best,

Here's the question you should answer: why should it not be on Object?



This kind of thinking can really help solve a lot of problems even when applied to this question.
Carl Gustav Jacob Jacobi


One of his maxims was: 'Invert, always invert' ('man muss immer umkehren'), expressing his belief that the solution of many hard problems can be clarified by re-expressing them in inverse form.


 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Abdulla S Mamuwala wrote:Truly, I thought I was communicating with a human and not a computer!



What's the point of that comment? I would have thought it would be obvious that A) You are currently communicating with humans, B) Much of what people ask questions about here involves communicating with computers, and C) I was talking about point B.

dictionary
Definitions on nitpicking,
verb (used without object)
1. to be excessively concerned with or critical of inconsequential details.
verb (used with object)



That's the closest of those definitions to what I meant by nitpicking. I would change it though to "...seemingly inconsequential."

Honestly, all I'm trying to do is to make sure that the OP (and anybody else interested or confused) understands why toString() is defined on Object and why arbitrary methods like description() defined on other classes are not equivalent to or replacements for toString(). You seem to be taking my comments personally, but there is nothing personal behind them.
 
Bartender
Posts: 1051
5
Hibernate Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

say my client wants to use the Car class I built and wants to know the description of the car from the Car object he builds. He prefers to know this description by calling a method named "description" which is more english like and more intuitive then "toString()". Then should I force him to use toString()


But in this instance, you wouldn't force the client to use toString and I don't think anyone here has suggested you should. If however, your client wanted a String representation of his Car object, showing all its state (say make, model, registration number, description), then you would override Object#toString() and the client could call that.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Abdulla S Mamuwala wrote:This kind of thinking can really help solve a lot of problems even when applied to this question.
...
One of his maxims was: 'Invert, always invert' ('man muss immer umkehren')


Hmmm. Not so sure that he was referring to this particular kind of problem (although I quite agree that it's a very useful practice - and I particularly like the German original - and not taught enough in programming classes in my view).

This is a simple definition problem: what is toString() defined to do?

Answer: convert an object to String form (for the rest, read the docs).

So (as Bear pointed out) the question is not why is the method defined in Object; the question is why wouldn't it be? And that's simple logic.

Winston
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hi am java and php programmer , but java is my Fav language.

your question is good .

Ans: Object class is play the important role in java . if object class is not is super class in java , you cannot synchronize the threads in java programs because Thread class inherits methods are (wait , notify(), notifyall())

2) One most important method in object class is toString() useful for Object representation in form of string .


3) there different forms of tostring methods are

- tostring() : without args

- toXXXString() : static methods for collection(java .utill)

- tostring(int i , int radix ) these method used for convert int to (binary , oct, hex)



That why object is need in java as super classs.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sourav jain wrote:if object class is not is super class in java , you cannot synchronize the threads in java programs because Thread class inherits methods are (wait , notify(), notifyall())



Er, no.

We don't "synchronize threads," and even if you want to use that terminology, the fact that Thread inherits those methods is irrelevant. What matters is that all classes inherit them. And if we didn't have an Object class or didn't have those methods defined in Object, there are other ways to achieve the same results.


3) there different forms of tostring methods are

- tostring() : without args

- toXXXString() : static methods for collection(java .utill)

- tostring(int i , int radix ) these method used for convert int to (binary , oct, hex)



The latter two have nothing to do with this thread or with Object's toString() method.
reply
    Bookmark Topic Watch Topic
  • New Topic