Skip to content

Commit 01dc531

Browse files
committed
docs(generics): add comprehensive explanation of Java Generics with type erasure details
WHAT: - Added a detailed conceptual explanation of Generics in Java. - Covered definitions, advantages, disadvantages, and real-world usage. - Explained how Generics enable parameterized types for methods, classes, and interfaces. - Compared Generics in Java to C++ templates. - Added notes on type parameters (T, E, K, N, V) and their conventions. - Included explanation of type erasure and its implications at runtime. - Provided example with List<String> vs List<Integer> to demonstrate type erasure. WHY: - Generics are a cornerstone of modern Java programming, ensuring type safety and reducing runtime errors. - Developers often misuse raw types, leading to ClassCastException issues; this documentation clarifies safe usage. - Explains how Generics improve code reusability, readability, and maintainability. DETAILS: - Generic methods: Define reusable methods with type parameters. - Generic classes: Define reusable data structures with type parameters. - Advantages: Code reusability, type safety at compile time, no explicit casting, better readability, generic algorithms. - Disadvantages: Added complexity for beginners, performance overhead due to type erasure, no direct support for primitive types, limited reflection support. - Type erasure: Generic type information is removed at runtime for backward compatibility. Example given showing List<String> and List<Integer> share the same runtime class. BENEFITS: - Encourages writing generic algorithms and libraries that work across types. - Prevents runtime errors by enforcing type checks at compile time. - Makes Java code more robust and consistent with modern programming practices. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent 0794b58 commit 01dc531

File tree

1 file changed

+36
-23
lines changed

1 file changed

+36
-23
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,45 @@
1-
Generics means parameterized types. The idea is to allow a type (like Integer, String, etc., or user-defined types) to be a parameter to methods, classes, and interfaces. Using Generics, it is possible to create classes that work with different data types. An entity such as a class, interface, or method that operates on a parameterized type is a generic entity.
1+
Generics mean parameterized types.
22

3-
Or
3+
The idea is to allow a type (like Integer, String, etc., or user-defined types) to be a parameter to methods, classes,
4+
and interfaces.
5+
6+
Using Generics, it is possible to create classes that work with different data types.
7+
8+
An entity such as a class, interface, or method that operates on a parameterized type is a generic entity.
49

5-
Generics in Java introduce parameterized types, allowing classes, interfaces, and methods to operate on different data types while ensuring type safety at compile time.
10+
Or
611

7-
Generics in Java are similar to templates in C++. For example, classes like HashSet, ArrayList, HashMap, etc., use generics very well.
12+
Generics in Java introduce parameterized types, allowing classes, interfaces, and methods to operate on different data
13+
types while ensuring type safety at compile time.
814

15+
Generics in Java are similar to templates in C++.
16+
For example, classes like HashSet, ArrayList, HashMap, etc., use generics very well.
917

1018
Type Parameters in Java Generics:
1119

1220
The type parameters naming conventions are important to learn generics thoroughly.
1321

1422
The common type parameters are as follows:
15-
1623
T – Type
1724
E – Element
1825
K – Key
1926
N – Number
2027
V – Value
2128

29+
Types of Java Generics:
30+
Generic Method: Generic Java method takes a parameter and returns some value after performing a task.
31+
It is exactly like a normal function, however, a generic method has type parameters that are cited by actual type.
2232

33+
This allows the generic method to be used in a more general way.
2334

24-
Types of Java Generics
25-
26-
Generic Method: Generic Java method takes a parameter and returns some value after performing a task. It is exactly like a normal function, however, a generic method has type parameters that are cited by actual type. This allows the generic method to be used in a more general way.
35+
Generic Classes: A generic class is implemented exactly like a non-generic class.
36+
The only difference is that it contains a type parameter section. There can be more than one type of parameter,
37+
separated by a comma.
2738

28-
Generic Classes: A generic class is implemented exactly like a non-generic class. The only difference is that it contains a type parameter section. There can be more than one type of parameter, separated by a comma.
39+
Generic Class:
40+
Like C++, we use <> to specify parameter types in generic class creation.
2941

30-
31-
32-
** Generic Class
33-
Like C++, we use <> to specify parameter types in generic class creation. To create objects of a generic class, we use the following syntax.
42+
To create objects of a generic class, we use the following syntax.
3443

3544
// To create an instance of generic class
3645
BaseType <Type> obj = new BaseType <Type>()
@@ -42,25 +51,30 @@ Advantages of Generics:
4251

4352
Code Reusability: You can write a method, class, or interface once and use it with any type.
4453
Type Safety: Generics ensure that errors are detected at compile time rather than runtime, promoting safer code.
45-
No Need for Type Casting: The compiler automatically handles casting, removing the need for explicit type casting when retrieving data.
54+
No Need for Type Casting: The compiler automatically handles casting, removing the need for explicit type casting when
55+
retrieving data.
4656
Code Readability and Maintenance: By specifying types, code becomes easier to read and maintain.
47-
Generic Algorithms: Generics allow for the implementation of algorithms that work across various types, promoting efficient coding practices.
57+
Generic Algorithms: Generics allow for the implementation of algorithms that work across various types,
58+
promoting efficient coding practices.
4859

4960
Disadvantages of Generics:
5061

5162
Complexity: For beginners, understanding concepts like wildcards (? extends, ? super) can be difficult.
5263
Performance Overhead: Type erasure causes some overhead as generic types are converted to Object during runtime.
53-
No Support for Primitive Types: Generics only work with reference types, requiring the use of wrapper classes like Integer or Double for primitives.
54-
Limited Reflection: Type erasure limits how much you can use reflection with generics since type information is not available at runtime.
64+
No Support for Primitive Types: Generics only work with reference types, requiring the use of wrapper classes like
65+
Integer or Double for primitives.
66+
Limited Reflection: Type erasure limits how much you can use reflection with generics since type information is not
67+
available at runtime.
5568
Programs that use Generics has got many benefits over non-generic code.
5669

5770
1. Code Reuse: We can write a method/class/interface once and use it for any type we want.
71+
2. Type Safety: Generics make errors to appear compile time than at run time (It’s always better to know problems in
72+
your code at compile time rather than making your code fail at run time).
5873

59-
2. Type Safety: Generics make errors to appear compile time than at run time (It’s always better to know problems in your code at compile time rather than making your code fail at run time).
60-
61-
Suppose you want to create an ArrayList that store name of students, and if by mistake the programmer adds an integer object instead of a string, the compiler allows it. But, when we retrieve this data from ArrayList, it causes problems at runtime.
62-
74+
Suppose you want to create an ArrayList that store name of students, and if by mistake the programmer adds an integer
75+
object instead of a string, the compiler allows it.
6376

77+
But, when we retrieve this data from ArrayList, it causes problems at runtime.
6478

6579
In Java, generic type information is completely erased at runtime. This process is known as type erasure.
6680

@@ -80,8 +94,7 @@ System.out.println(stringList.getClass() == intList.getClass()); // prints true
8094

8195
At runtime, both stringList and intList are just List objects, without any information about their generic type.
8296

83-
8497
This approach was chosen to maintain backward compatibility with pre-generics code.
8598

8699
However, it also means that certain operations, like checking if an object is an instance of a specific generic type,
87-
are not possible at runtime.
100+
are not possible at runtime.

0 commit comments

Comments
 (0)