Interview Focused : 10 Points on Java Generics
1) Simply put generics try to stops bugs creeping in your code and bursting it at runtime by putting compile time checks.
2) Generics help to make a class or method generalized enough to be used with a lot more types of objects.
3) You don’t need to write 10 overloaded methods to handle 10 type of objects, one will solve the problem.
4) Type erasure is used for backward compatibility, hence at runtime you cannot make out what type was intended to be used as compiler removes all the generics associated data from the class file.
5)One big let off for generics is the confusing syntax which make code readability worse.Also wild-cards are not helping the code understandability.
6) Bridge method are synthetic methods created sometimes while compiling class or interface which extends a parameterized class to interface.Compiler generates a bridge method to ensure that sub-typing works as expected.
7) A non-reifiable type does not have all of its information available at runtime. Examples of non-reifiable types are List and List; the JVM cannot tell the difference between these types at runtime.
8)Heap pollution occurs when a variable of a parameterized type refers to an object that is not of that parameterized type, example, heap pollution occurs when mixing raw types and parameterized types, or when performing unchecked casts.
9)Generics cannot instantiate generic types with primitive types,or declare static fields whose types are type parameters.
10)Unknown types are accommodated using upper,unbounded and lower bound wild-cards, where unknown is represented using ?.

Comments
Post a Comment