Mastering Java Core Concepts: Generics, Collections, Events, and Swing
Classified in Computers
Written on in
English with a size of 6.82 KB
Java Generics: Methods and Classes
Java Generics enable programmers to specify, with a single method declaration, a set of related methods, or with a single class declaration, a set of related types, respectively. Generics also provide compile-time type safety, which allows programmers to catch invalid types at compile time, preventing runtime errors.
Generics Example: ArrayList
Consider the following example demonstrating generics with an ArrayList:
1. import java.util.*;
2. class TestGenerics1 {
3. public static void main(String args[]) {
4. ArrayList<String> list = new ArrayList<String>();
5. list.add("rahul");
6. list.add("jai");
7. // list.add(32); // compile-time error if uncommented
8. String s = list.get(... Continue reading "Mastering Java Core Concepts: Generics, Collections, Events, and Swing" »