This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Java in General and the fly likes Difference between Properties class and HashTable,HashMap Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Difference between Properties class and HashTable,HashMap" Watch "Difference between Properties class and HashTable,HashMap" New topic
Author

Difference between Properties class and HashTable,HashMap

Nikhil Sun
Ranch Hand

Joined: Nov 13, 2005
Posts: 108
I have the following key value pairs:
accountnumber 1
accounttype 2
status 3

These are stored in HashMap,HashTable.Also I use Properties class which loads these values from a property file.

Now I do the following within my code:

1)int i = ((Integer)hashTable.get("accountnumber")).intValue();

2)int j = ((Integer)hashMap.get("accountnumber")).intValue();

3)int k = ((Integer)properties.get("accountnumber")).intValue();

However I get a ClassCastException in the third case..Why?How does it differ from the previous 2 cases.

Also in the first 2 cases how can I cast the Object returned by get method to Integer?Doesn't that amount to narrow casting which should give ClassCasException??

Kindly clarify my doubts.
Nitesh Kant
Bartender

Joined: Feb 25, 2007
Posts: 1638

Properties only store values as strings. So, you can not cast it to Integer. You can use a Integer.parseInt() for getting int from the string value.


apigee, a better way to API!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Difference between Properties class and HashTable,HashMap
 
Similar Threads
Wrapper Class.
Banking with Threads
boxing,unboxing,for each and varargs
overloaded func, both accept same no and type of parameters
Question on sum of syracuse sequence (memorization question)