Post a Job  |   Ask a Question?   |   Already registered? login or register
ram sai
15/03/2016 11:06 PM

Core Java


Friends.. Can anyone please explain me why we get erasure error in Java, when we write a method with same method name and same return type and with for first method List<String> and for second method List<Integer> as parameters.... Please clear my doubt....
Spam    1 Comments

Mridu,M
16/03/2016 00:21:00
The compiler generates only one byte code representation of a generic type or method unique representation. This mapping is performed by type erasure. ie When the compiler finds the definition of a generic type or method, it removes all occurrences of the type parameters and replaces them by their leftmost bound, or type Object if no bound had been specified. thus during type erasure the type arguments are discarded and the type parameters are replaced by their leftmost bound. For Eg the types List<String> , Set<Long> , and Map<String,?> are translated to List , Set and Map respectively. The JVM cannot tell the difference between a List<String> and a List<Integer> and a List<Date> Since after type erasure methods will become duplicate (same, name, and same return types ) with parameters as List in both methods since <String> and <Integer> is discarded. ,The compiler generates only one byte code representation of a generic type or method to the unique representation. This mapping is performed by type erasure, where when the compiler finds the definition of a generic type or method, it removes all occurrences of the type parameters and replaces them by their leftmost bound, or type Object if no bound had been specified. ie during type erasure the type arguments are discarded and the type parameters are replaced by their leftmost bound. For eg. the types List<String> , Set<Long> , and Map<String,?> are translated to List , Set and Map respectively. A side effect of type erasure is that the virtual machine has no information regarding type parameters and type arguments. The JVM cannot tell the difference between a List<String> and a List<Integer> and a List<Date> After removal of generic type ie <String> and <Integer> methods will become duplicate so obviously duplicate methods will not allowed.

Languages :
xml