generics 2 Answer(s) 5 years and 9 months ago
Posted in : Java Beginners
June 18, 2009 at 11:52 AM
If you want to take an element from the Collection, you have to cast the type of element that is stored in the collection.The compiler does not check that your cast is the same as the collection's type or not, which makes the cast fail at run time.To overcome this situation Generics provides a way to communicate the collection type to the compiler which aware the compiler about the element type of the collection, and can insert the correct casts on values being taken out of the collection.
Generics provide a mechanism for annotating type information for the use of the compiler. By providing this information, the compiler can in turn verify the consistent use of "genericized" classes and automatically insert explicit casts, eliminating the need for programmers to do so manually.
The Generics makes its information available only at compile time and not translated into the compiled bytes. This allows genericized classes to interoperate cleanly with legacy, or raw, types.
It has the ability for library authors to express their intent more cleanly. Generics help to enforce the contents of collections and other genericized classes.
Generics can be used in more than just collection classes which allows developers to write their own genericized classes and genericized methods with generics.
September 13, 2011 at 8:13 PM
i am looking to develop mobile application project for some good practice, please advice me.
Why Generics was introduced in Java?
Why Generics was introduced in Java? Hi,
Why Generics was introduced in Java and what is was its motive?
Generics were... for compile time type checking.
Read the tutorial for more details of Generics