This type of coding is very risky! Although it will compile, you will notice that the compiler complains about the warning:
Note. NoRulesForMe.java uses unverified or unsafe operations.
Note. Recompiling with -Xlint: unchecked for details.
These warnings, especially because you are using generics, should never be ignored or simply suppressed. You must absolutely be sure (logically following the code) that the roll is safe and will not cause some problems later. It's best to always code so that errors are detected and collected at compiler time instead of runtime. A warning given by the compiler tells you that everything may go wrong.
You pass myMap as an Object to castWildly , and when you cast, you drop from Object to Map .
The compiler can conclude that T in your code has the target type Map<String, Double> and, therefore, can do it. However, when casting, it has no information about which (sub) type is Object value (or Object theRing ). Thus, it has no way to verify that casting is safe (especially type safe).
A problem with this code occurs when you retrieve values ββfrom your map. The following code has one extra line added, and the code compiles (with the same warning as above). This is because when retrieving a Double value from a map declared as Map<String, Double> , it is absolutely valid when the compiler checks the type ... at runtime, however, your code will work (a runtime failure error is displayed below ) This is a very dangerous coding method, especially in production code. You would rather have your compiler give you errors than deploying production code that compiles and crashes your product in real time.
public class NoRulesForMe { static Object theRing; public static void main(String[] args){ Map<Integer, String> myMap = new HashMap<>(); myMap.put(9,"star"); Map<Integer, Double> myMapMorphed = castWildly(myMap); myMapMorphed.put(99, 3.14); System.out.println(myMapMorphed.get(9));
Runtime error when running the above code:
star
3.14
An exception in the "main" thread java.lang.ClassCastException: java.lang.String cannot be added to java.lang.Double on NoRulesForMe.main (NoRulesForMe.java:19)
For more information, read Joshua Bloch's "Effective Java"; Paragraph 24: Eliminate unverified warnings. (This item is under the heading Generics).