• Post Reply Bookmark Topic Watch Topic
  • New Topic

Using HashMap  RSS feed

 
Daniel .J.Hyslop
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a small program here where I am using a HashMap object with a Map Reference and a Vector object with a List reference. The later works fine when passing it as a methods type to the main method.however the HashMap objects keys are output as Strings fine but the values which come from the constructors of another class do not .The program complies ok ,It is just the output which is confusing me , here`s the code:
 
Daniel .J.Hyslop
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I must just add the package and import statments are present in class a1 I must have missed them in the paste
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The version of System.out.println() that takes an Object as a parameter automatically calls the toString() method. The default implementation of toString() produces the output that you see here. For more details, see the javadocs for Object.toString(). If you wrote this class yourself, you should override the toString() method to print useful information about the object.

HTH

Layne
 
Daniel .J.Hyslop
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi layne,
Thanks for the prompt reply as usual, just to cement my understanding of what is happening here , when you say System.out.println(foo); implements the Object classes toString method does this mean that when I create my own toString method in a2 that System...out(foo)automatically will implement that one.I have updated a2 with a toString() method and it does work :see below

Just another point on this , if my assumption is correct on the actions ive described , why does s2.getMap(); in the main method output the correct data if it does not have access to the toString method?

Thanks Danny

[ August 16, 2005: Message edited by: Daniel .j.Hyslop ]
[ August 16, 2005: Message edited by: Daniel .j.Hyslop ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!