SCJP Certification

Sun Certified Java Programmer Certification exam essentials

August 28th, 2009

Generics

Java Facts, by Daisy Williams.

Generics is the most powerful feature of J2SE 5. It is most useful with collections such as Set and List. Collections are bound to contain a specific type of object. It allows a Java programmer to abstract over types. Therefore, if a programmer tries to insert any element other than the specified type into the collection, a compile time error will be thrown.

For example, a method declared as static void genericsexample(Collection c) will allow only strings to be inserted into the collection.

Generics has the following advantages:

  • No typecasting is required when an element is taken out of a collection.
  • Code is checked at the compile time rather than the runtime, so no ClassCastException occurs.
Share

Back Top

Responses to “Generics”

Comments (0) Trackbacks (0) Leave a comment Trackback url
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Your email address will not be published. Required fields are marked *

*