This week's book giveaway is in the OCP forum.
We're giving away four copies of OCP Java SE 8 Programmer II Exam Study Guide and have Kathy Sierra, Bert Bates, & Elizabeth Robson on-line!
See this thread for details.
Win a copy of OCP Java SE 8 Programmer II Exam Study Guide this week in the OCP forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Please solve my problem  RSS feed

 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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]
 
Sheriff
Posts: 9610
37
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Faisal! Welcome to javaranch.

Are you trying to ask something like this??
 
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Marshal
Posts: 58421
178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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().


 
author and iconoclast
Sheriff
Posts: 24218
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!