Share on Google+Share on Google+

2 Answer(s)      8 years ago
Posted in : Java Beginners
View Answers

June 18, 2009 at 11:52 AM

Hi Friend,

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

hello sir/madam,

i am looking to develop mobile application project for some good practice, please advice me.



Related Tutorials/Questions & Answers:
eraser type generics in java
eraser type generics in java  Explain eraser type generics in java with example
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? Thanks   Generics were... for compile time type checking. Read the tutorial for more details of Generics


Advertisement null