IntelliJ Java IDE
The moose likes Other JSE/JEE APIs and the fly likes put() method in Hashtable/Hashmap. Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Reply Bookmark "put() method in Hashtable/Hashmap." Watch "put() method in Hashtable/Hashmap." New topic
Author

put() method in Hashtable/Hashmap.

Kaustubh Patil
Ranch Hand

Joined: Aug 13, 2001
Posts: 164
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.


Kaustubh. Mumbai, India.
Mark Herschberg
Sheriff

Joined: Dec 04, 2000
Posts: 6035
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

Joined: Aug 13, 2001
Posts: 164
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.
 
IntelliJ Java IDE
 
subject: put() method in Hashtable/Hashmap.
 
Threads others viewed
Key with same hashcode
General question about HashMaps in java
Generics error, can not understand
Using HashMap vs Hastable
hashmap and equals doubts
IntelliJ Java IDE

cast iron skillet 49er

more from paul wheaton's glorious empire of web junk: cast iron skillet diatomaceous earth rocket mass heater sepp holzer raised garden beds raising chickens lawn care CFL flea control missoula heat permaculture