• 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

Object.toString()

 
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
During my travels down the cattle path and through OOP-4 (Lookup) i ended up printing an Object.toString() which produced Video@20c10f (or BookOnTape@20c10f or Furniture etc). I brought this up with Marilyn and she suggested i post it here to get an explanation of why this happened??
So please could someone explain why this might happen and what the code is that is printed up.
(Incidentally i sorted out what i was doing wrong in my program and it works like a charm i was just curious as to what was printed and why)
Thanks
Sam
 
Ranch Hand
Posts: 351
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'd encourage you to look in the J2SE API and read the gorry details for the toString() method.
The short story I recall is if you invoke toString() on an Object it will print className@hashCode.
I doubt this information will be required very often. A nice question for the SCJP exam or trivia to impress your friends with.
[ April 11, 2002: Message edited by: Michael Pearson ]
 
whippersnapper
Posts: 1843
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I remember posting on this once before in this forum. Here's the discussion. The relevant bit is about halfway down.
 
Chicken Farmer ()
Posts: 1932
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
toString() is used excessively in the Java world. I would suggest what the others said and read the API, and learn how to use it in your classes. You won't get far without knowing how this little method can help you
 
Michael Matola
whippersnapper
Posts: 1843
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Advanced (ok, maybe intermediate) toString()ing:
Track down why the code at (A) produces the output "null" and the code at (B) throws a NullPointerException:
 
jason adam
Chicken Farmer ()
Posts: 1932
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I know! I know!
 
Sheriff
Posts: 4012
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Pick me! Pick me! (I had to track it down after all, Jason just knew it.)
Since no one else has jumped at this, I'm going for it. OK Jason?

For the first case (A):
From the API java.io.PrintWriter or java.io.PrintStream for the method println(Object obj):
println( Object obj) "behaves as if it invokes print(obj) and then println()."
The method print( Object obj) returns the string produced by String.valueOf( Object obj ).
So in turn, the method String.valueOf( Object obj) "returns the string representation of the Object argument."
Here's the nuance. Just exactly what it returns depends on the argument:
"if the argument is null, then the string equal to "null" ; otherwise, the value of obj.toString() is returned."

For the second case (B):
The value of the variable here named object is null, which means it is a reference to nothing, since null is used to indicate the absence of an object. There is no object on which to call an instance method, in this case toString(); hence the NullPointerException.
.....
A side note...
As someone who prefers reading the printed page to starting the computer and reading from the screen, I went and looked at 6 (count 'em 6!) different books before giving up and going to the API. 5 minutes later there it was. Have to work on changing my habits and going to the API first.
[ April 13, 2002: Message edited by: Pauline McNamara ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic