Java Generics Concepts: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 3: Line 3:
=Overview=
=Overview=
'''Generics''', or '''generic types''', or '''parameterized types''' are a Java language extension and a set of compiler features introduced in [[Java#Java_5|Java 5]] and improved in subsequent Java releases, which allow writing more reliable code by making certain categories of bugs detectable at compile time.
'''Generics''', or '''generic types''', or '''parameterized types''' are a Java language extension and a set of compiler features introduced in [[Java#Java_5|Java 5]] and improved in subsequent Java releases, which allow writing more reliable code by making certain categories of bugs detectable at compile time.
Generics enable types (classes and interfaces) and individual methods to be parameterized with other types when they are declared in the source code. Type parameters are formal parameters of a type or a method much like a function parameters are formal parameters of a function, declared in the function signature. Type parameters provide a way to re-use the same code with different inputs - other types in this case.


=Type Inference=
=Type Inference=

Revision as of 21:19, 14 September 2021

Internal

Overview

Generics, or generic types, or parameterized types are a Java language extension and a set of compiler features introduced in Java 5 and improved in subsequent Java releases, which allow writing more reliable code by making certain categories of bugs detectable at compile time.

Generics enable types (classes and interfaces) and individual methods to be parameterized with other types when they are declared in the source code. Type parameters are formal parameters of a type or a method much like a function parameters are formal parameters of a function, declared in the function signature. Type parameters provide a way to re-use the same code with different inputs - other types in this case.

Type Inference

Java Type Inference

Organizatorium

Covariance

https://en.wikipedia.org/wiki/Covariance_and_contravariance_(computer_science)

Let T and S be two types (class or function types), such that S is a subtype of T. If method m of T is overridden in S, then the corresponding types from the m's signature can either preserve the relationship between T and S (the type used in S is a subtype of the corresponding type in T), reverse the relationship (the type used in S is a super type of the type used in T), or neither preserve nor reverse this relationship. If they preserve the relationship to T and S, we say they are covariant, if they reverse the relationship of T and S, we say they are contravariant.