Advanced Java Tutorial


Total available pages count: 55
Subject - Java Technologies

Generics

The Generics used to deal with compile type-safe object & avoid the chances of ClassCasteException. The Generics make code stable by detecting bugs/errors at compile time.

Normally you can store any type of object in collections i.e., generic or non-generic. The Generics facilitated the programmer to store a specific type of object.
 

Advantages

Compile type safety
You can hold a single type of object and it doesn’t allow you to store another object.

Compile-time checking
The Compile-time checking is checked at compile time so none of any problem occurs at runtime. A good coding strategy says it is far better to handle problems at compile time than runtime.

/*Generics collection syntax */
ClassOrInterface<Type>

/*Example */
Arraylist<String>

 

Typecasting is not required because there is no need to do typecast the object.



Comments