Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Please solve my problem

 
faisal hameed
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]
 
Ankit Garg
Sheriff
Posts: 9520
22
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??
 
Sachin Adat
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.

 
Campbell Ritchie
Sheriff
Posts: 48921
58
  • 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.
 
Maneesh Godbole
Saloon Keeper
Posts: 11027
12
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().

 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
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