Java Generics Concepts: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 41: Line 41:
<font size=-1>
<font size=-1>
  <font color='green'>class</font>|<font color='green'>interface</font> <font color='blue'>name</font><T1, T2, ...Tn> {
  <font color='green'>class</font>|<font color='green'>interface</font> <font color='blue'>name</font><T1, T2, ...Tn> {
   <font color='teal'>// ...</font>
   <font color='gray'>// ...</font>
  }
  }
</font>
</font>

Revision as of 22:32, 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, type-safe code. Generics make certain categories of type-related 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 of a method much like a function parameters allow the function code to produce different results when invoked in the presence of different arguments. Type parameters provide a way to re-use the same code with different inputs - other types in this case.

public class SomeClass<T> {
  public T doSomething(T t) {
    System.out.println(t);
    return t;
  }
  public <V> V doSomethingElse(V v) {
    System.out.println(v);
    return v;
  }
}

public class Main() {
  public static void main(String[] args) {

    SomeClass<String> scs = new SomeClass<>();
    String s = scs.doSomething("something"); // no cast
    // scs.doSomething(new Object()); // compile-time failure: "error: incompatible types: Object cannot be converted to String"

    SomeClass<Integer> sci = new SomeClass<>();
    Integer i = sci.doSomething(1); // no cast

    Double d = sci.doSomethingElse(1.0d); // no cast
  }
}

Java code that uses generics has several benefits over non-generic code:

  • Allows for stronger type checks at compile time.
  • Removes the need to cast.
  • Enables programmers to implement generic algorithms - algorithms that work on collections of different types, can be customized, are type safe and easier to read.

Generic Type

A generic type or a parameterized type is a class or an interface that is parameterized over types.

The syntax of a parameterized type is:

class|interface name<T1, T2, ...Tn> {
  // ...
}

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.