0% found this document useful (0 votes)
4 views

Generics in Java

Generics in Java allow type-safe operations on collections by making the type of objects in collections explicit. This avoids unsafe casts when retrieving objects from collections. Generics were introduced in JDK 5 and use angle brackets <> to specify types.

Uploaded by

gihanya2000
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Generics in Java

Generics in Java allow type-safe operations on collections by making the type of objects in collections explicit. This avoids unsafe casts when retrieving objects from collections. Generics were introduced in JDK 5 and use angle brackets <> to specify types.

Uploaded by

gihanya2000
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

ENTERPRISE APPLICATION

DEVELOPMENT-1
Tharanga Ekanayake
BSc (Special) MIS, First Class
Generics in Java
• Generics in java were introduced as one of features in JDK 5
• The angular brackets “<>” used in generics
Java Generics Example
*The List interface represents a list of Object instances. This means that we could put
any object into a List. Here is an example:

List list = new ArrayList();

list.add(new Integer(2));
list.add("a String");

*Because any object could be added, you would also have to cast any objects
obtained from these objects. For instance:

Integer integer = (Integer) list.get(0);

String string = (String) list.get(1);

You might also like