This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes toString method in Object Class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "toString method in Object Class" Watch "toString method in Object Class" New topic
Author

toString method in Object Class

pratik gaurav
Greenhorn

Joined: Oct 04, 2012
Posts: 16

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
Abdulla S Mamuwala
Greenhorn

Joined: Jul 27, 2009
Posts: 21
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!
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60766
    
  65

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


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Ishan Pandya
Ranch Hand

Joined: Feb 06, 2012
Posts: 219

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.


OCPJP
Rajdeep Biswas
Ranch Hand

Joined: Mar 26, 2012
Posts: 186

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


The biggest gamble will be to ask a question whose answer you know in that it will challenge your theory | www.TechAspire.blogspot.in
Abdulla S Mamuwala
Greenhorn

Joined: Jul 27, 2009
Posts: 21
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?
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Oct 04, 2012
Posts: 16

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
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37943
    
  22
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

Joined: Jul 27, 2009
Posts: 21
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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Jul 27, 2009
Posts: 21
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

Joined: Jul 27, 2009
Posts: 21
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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Jul 27, 2009
Posts: 21
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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Jul 27, 2009
Posts: 21
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"!
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7518
    
  18

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

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7518
    
  18

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

Joined: Jul 27, 2009
Posts: 21
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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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.
James Boswell
Bartender

Joined: Nov 09, 2011
Posts: 1012
    
    5

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

Joined: Mar 17, 2011
Posts: 7518
    
  18

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
sourav jain
Greenhorn

Joined: Nov 19, 2012
Posts: 22

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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: toString method in Object Class
 
Similar Threads
Regarding String Literals And String Objects
why new date() prints date ?????
Why do String class have toString method?
about toString() method
overridding instance method with static method