aspose file tools*
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
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