• Post Reply Bookmark Topic Watch Topic
  • New Topic

put() method in Hashtable/Hashmap.  RSS feed

 
Kaustubh Patil
Ranch Hand
Posts: 164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
the put() method in Hashtable/Hashmap returns a object according to the specification.
But even if u just code as hashtable.put() and do no assign the returned object to anything (that is treat the method as returning void) the code still works perfectly ..
Why does this happen ??
Thanks,
Kaustubh.
 
Mark Herschberg
Sheriff
Posts: 6037
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Basically, you'are asking the following. Given a method foo() which returns an object, what's the difference between.

In both cases, you call foo(). In both cases, the method works as expected, and in both cases, an object is returned. In the first case, that object is referenced by the variable obj. In the second case, no reference to the object is given. When the method returns, the reference to the object within the method goes out of scope, and is ultimately "dropped." So effectively, you created an object (assuming the method itself created the object), that no has no references, and will be garbage collected.
Effectively, the latter statement is equivalent to


--Mark
 
Kaustubh Patil
Ranch Hand
Posts: 164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So does this mean that it can happen with any method or just some special methods ?
i.e. is it OK if i ignore the object returned from any method ?
Kaustubh.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!