File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Please solve my problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Please solve my problem" Watch "Please solve my problem" New topic

Please solve my problem

faisal hameed
Ranch Hand

Joined: Jan 19, 2009
Posts: 48
why toString() is called when i create object of Box "b".
when i call System.out.println(b); it display Dimensions are 10.0 by 14.0 by 12.0
why it display.

[edit]Add code tags and correct indentation. CR[/edit]

Regards, M Faisal Hameed
PUCIT, Pakistan
Ankit Garg

Joined: Aug 03, 2008
Posts: 9466

Hi Faisal! Welcome to javaranch.

Are you trying to ask something like this??

SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Sachin Adat
Ranch Hand

Joined: Sep 03, 2007
Posts: 213
Hi Faisal,
Welcome to Java Ranch!!!
Please use code buttons(its very difficult to read your message) and quote your source
though I guess its from K&B.

How To Ask Questions On Java Ranch - How To Answer Questions On Java Ranch
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46412
Welcome to JavaRanch.

Please don't call a class "string" because there is already a String class and you can get no end of confusion between the two. I have altered your indentation and added code tags (as somebody suggested) so you can see how much better the code looks. Please get a decent text editor (Google for NotePad2 and NotePad++) and use that; it is a lot better for coding that NotePad. Set the options to "replace tabs with spaces" and "tab setting = 4 spaces" which gives better indentation for Java. Always use thread headings which tell us what the thread is about.

Now to the question you are actually asking. Ankit Garg is probably correct in his link; I shall give you some more links.

You need to know how System.out.println() works and what it does, so you need to look in the API for System, then near the top there is a box with "field summary" written on, and you find "out" in there, with something like standard output stream against it. To the left of "out" it says "PrintStream;" click on that, then find the println method (click on "METHOD" at the top if you have any difficulty), then the version of println called println(Object x). Click on that, and it will give you the explanation. You may need this link as you explore through the API documentation.

Once you have been through all that, you should know what is happening. If not, please ask again.
faisal hameed
Ranch Hand

Joined: Jan 19, 2009
Posts: 48
Hi, I am student of MSc IT.
I want to know that how toString method is called automatically
when I Print the object.Please give me detail of toString method.
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10828

From the API docs

Prints an Object and then terminate the line. This method calls at first String.valueOf(x) to get the printed object's string value, then behaves as though it invokes print(String) and then println().

[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

It's the println(Object) method that's automatically calling toString(). PrintStream.println(Object) looks like this:

It calls the static method String.valueOf(Object) to convert the Object to a String. And String.valueOf(Object) just looks like this:

As you can see, it calls toString() on your object. So no magic!

[Jess in Action][AskingGoodQuestions]
I agree. Here's the link:
subject: Please solve my problem
jQuery in Action, 3rd edition