In Java 1.5 why the name "Generics" even though it extends Collections which hold special types pf objects

StackOverflow https://stackoverflow.com/questions/18523588

  •  26-06-2022
  •  | 
  •  

Question

Java 1.5 onwards we have support for Generics which allows defining Collections to hold special types of objects like List<String> list; instead of List list;,

Why did Sun chose to name this as "Generics" even though it specializes the behavior in which collections are used?

Was it helpful?

Solution

Because it allows you to write generic classes, that can hold generic data types, which are only specialized when the class is actually instantiated.

List<String> is indeed a specialization, but the only class that has been written is List<E>, which is generic.

OTHER TIPS

The term was actually originated in the Standard Template Library for C. A "generic" is called such because it is NOT specific to a particular data type. It is a template that can contain ANY data type that you specify at the time you write the code. It is exactly as you said, but you are thinking about it from the way it is instanced. The class itself is actually generic and you can create any type of specific instance from it.

"Generics" is not a Java specific term. The methods contained in the collections are general, working for all types of classes, i. e. they are "generic". The workaround before Java 1.5 was to use class Object but this was not type safe.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top