The Properies class in Java SE 6 has a method called setProperty(String key, String value), which returns an Object. Furthermore, the previous Object stored for this key, or NULL if none exists.
Since setProperty(String key, String value) can only take a String as value, why doesn’t that method return a String?
The Properies class in Java SE 6 has a method called setProperty(String key, String
Share
Unfortunately class
java.util.Propertieswas introduced into java 1.0, many years before generics.PropertiesextendsHashtablethat can store any type of data. So, you can do the following:In this example
setPropertymust return the previous value stored in this entry, i.e.Object. But it is notString! To avoidClassCastExceptionthe JDK creators had to definesetProperty()as method that returnsObject.BTW even now class Properties implements
Map<Object, Object>instead ofMap<String, String>for backwards compatibility.