Parameterized java types of

Generic Types

A generic type is a generic class or interface that is parameterized over types. The following Box class will be modified to demonstrate the concept.

A Simple Box Class

Begin by examining a non-generic Box class that operates on objects of any type. It needs only to provide two methods: set, which adds an object to the box, and get, which retrieves it:

public class Box < private Object object; public void set(Object object) < this.object = object; >public Object get() < return object; >>

Since its methods accept or return an Object, you are free to pass in whatever you want, provided that it is not one of the primitive types. There is no way to verify, at compile time, how the class is used. One part of the code may place an Integer in the box and expect to get Integers out of it, while another part of the code may mistakenly pass in a String, resulting in a runtime error.

A Generic Version of the Box Class

A generic class is defined with the following format:

Читайте также:  Python detect memory leaks

The type parameter section, delimited by angle brackets (<>), follows the class name. It specifies the type parameters (also called type variables) T1, T2, . and Tn.

To update the Box class to use generics, you create a generic type declaration by changing the code «public class Box» to «public class Box ". This introduces the type variable, T, that can be used anywhere inside the class.

With this change, the Box class becomes:

/** * Generic version of the Box class. * @param the type of the value being boxed */ public class Box  < // T stands for "Type" private T t; public void set(T t) < this.t = t; >public T get() < return t; >>

As you can see, all occurrences of Object are replaced by T. A type variable can be any non-primitive type you specify: any class type, any interface type, any array type, or even another type variable.

This same technique can be applied to create generic interfaces.

Type Parameter Naming Conventions

By convention, type parameter names are single, uppercase letters. This stands in sharp contrast to the variable naming conventions that you already know about, and with good reason: Without this convention, it would be difficult to tell the difference between a type variable and an ordinary class or interface name.

The most commonly used type parameter names are:

  • E - Element (used extensively by the Java Collections Framework)
  • K - Key
  • N - Number
  • T - Type
  • V - Value
  • S,U,V etc. - 2nd, 3rd, 4th types

You'll see these names used throughout the Java SE API and the rest of this lesson.

Invoking and Instantiating a Generic Type

To reference the generic Box class from within your code, you must perform a generic type invocation, which replaces T with some concrete value, such as Integer:

You can think of a generic type invocation as being similar to an ordinary method invocation, but instead of passing an argument to a method, you are passing a type argumentInteger in this case — to the Box class itself.

Type Parameter and Type Argument Terminology: Many developers use the terms "type parameter" and "type argument" interchangeably, but these terms are not the same. When coding, one provides type arguments in order to create a parameterized type. Therefore, the T in Foo is a type parameter and the String in Foo f is a type argument. This lesson observes this definition when using these terms.

Like any other variable declaration, this code does not actually create a new Box object. It simply declares that integerBox will hold a reference to a "Box of Integer", which is how Box is read.

An invocation of a generic type is generally known as a parameterized type.

To instantiate this class, use the new keyword, as usual, but place between the class name and the parenthesis:

The Diamond

In Java SE 7 and later, you can replace the type arguments required to invoke the constructor of a generic class with an empty set of type arguments (<>) as long as the compiler can determine, or infer, the type arguments from the context. This pair of angle brackets, <>, is informally called the diamond. For example, you can create an instance of Box with the following statement:

For more information on diamond notation and type inference, see Type Inference.

Multiple Type Parameters

As mentioned previously, a generic class can have multiple type parameters. For example, the generic OrderedPair class, which implements the generic Pair interface:

public interface Pair  < public K getKey(); public V getValue(); >public class OrderedPair implements Pair  < private K key; private V value; public OrderedPair(K key, V value) < this.key = key; this.value = value; >public K getKey() < return key; >public V getValue() < return value; >>

The following statements create two instantiations of the OrderedPair class:

Pair p1 = new OrderedPair("Even", 8); Pair p2 = new OrderedPair("hello", "world");

The code, new OrderedPair , instantiates K as a String and V as an Integer. Therefore, the parameter types of OrderedPair's constructor are String and Integer, respectively. Due to autoboxing, it is valid to pass a String and an int to the class.

As mentioned in The Diamond, because a Java compiler can infer the K and V types from the declaration OrderedPair , these statements can be shortened using diamond notation:

OrderedPair p1 = new OrderedPair<>("Even", 8); OrderedPair p2 = new OrderedPair<>("hello", "world");

To create a generic interface, follow the same conventions as for creating a generic class.

Parameterized Types

You can also substitute a type parameter (that is, K or V) with a parameterized type (that is, List ). For example, using the OrderedPair example:

OrderedPairBox > p = new OrderedPair<>("primes", new Box(. ));

Источник

Parameterized java types of

ParameterizedType represents a parameterized type such as Collection. A parameterized type is created the first time it is needed by a reflective method, as specified in this package. When a parameterized type p is created, the generic type declaration that p instantiates is resolved, and all type arguments of p are created recursively. See TypeVariable for details on the creation process for type variables. Repeated creation of a parameterized type has no effect. Instances of classes that implement this interface must implement an equals() method that equates any two instances that share the same generic type declaration and have equal type parameters.

Method Summary

Methods inherited from interface java.lang.reflect.Type

Method Detail

getActualTypeArguments

Returns an array of Type objects representing the actual type arguments to this type. Note that in some cases, the returned array be empty. This can occur if this type represents a non-parameterized type nested within a parameterized type.

getRawType

getOwnerType

Returns a Type object representing the type that this type is a member of. For example, if this type is O.I , return a representation of O . If this type is a top-level type, null is returned.

Submit a bug or feature
For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Copyright © 1993, 2023, Oracle and/or its affiliates. All rights reserved. Use is subject to license terms. Also see the documentation redistribution policy.

Источник

Parameterized java types of

ParameterizedType represents a parameterized type such as Collection. A parameterized type is created the first time it is needed by a reflective method, as specified in this package. When a parameterized type p is created, the generic type declaration that p instantiates is resolved, and all type arguments of p are created recursively. See TypeVariable for details on the creation process for type variables. Repeated creation of a parameterized type has no effect. Instances of classes that implement this interface must implement an equals() method that equates any two instances that share the same generic type declaration and have equal type parameters.

Method Summary

Methods declared in interface java.lang.reflect.Type

Method Detail

getActualTypeArguments

Returns an array of Type objects representing the actual type arguments to this type. Note that in some cases, the returned array be empty. This can occur if this type represents a non-parameterized type nested within a parameterized type.

getRawType

getOwnerType

Returns a Type object representing the type that this type is a member of. For example, if this type is O.I , return a representation of O . If this type is a top-level type, null is returned.

Report a bug or suggest an enhancement
For further API reference and developer documentation see the Java SE Documentation, which contains more detailed, developer-targeted descriptions with conceptual overviews, definitions of terms, workarounds, and working code examples.
Java is a trademark or registered trademark of Oracle and/or its affiliates in the US and other countries.
Copyright © 1993, 2023, Oracle and/or its affiliates, 500 Oracle Parkway, Redwood Shores, CA 94065 USA.
All rights reserved. Use is subject to license terms and the documentation redistribution policy.

Источник

Interface ParameterizedType

A parameterized type is created the first time it is needed by a reflective method, as specified in this package. When a parameterized type p is created, the generic class or interface declaration that p instantiates is resolved, and all type arguments of p are created recursively. See TypeVariable for details on the creation process for type variables. Repeated creation of a parameterized type has no effect.

Instances of classes that implement this interface must implement an equals() method that equates any two instances that share the same generic class or interface declaration and have equal type parameters.

Method Summary

Methods declared in interface java.lang.reflect.Type

Method Details

getActualTypeArguments

Returns an array of Type objects representing the actual type arguments to this type. Note that in some cases, the returned array be empty. This can occur if this type represents a non-parameterized type nested within a parameterized type.

getRawType

getOwnerType

Returns a Type object representing the type that this type is a member of. For example, if this type is O.I , return a representation of O . If this type is a top-level type, null is returned.

Report a bug or suggest an enhancement
For further API reference and developer documentation see the Java SE Documentation, which contains more detailed, developer-targeted descriptions with conceptual overviews, definitions of terms, workarounds, and working code examples. Other versions.
Java is a trademark or registered trademark of Oracle and/or its affiliates in the US and other countries.
Copyright © 1993, 2023, Oracle and/or its affiliates, 500 Oracle Parkway, Redwood Shores, CA 94065 USA.
All rights reserved. Use is subject to license terms and the documentation redistribution policy.

Источник

Parameterized java types of

ParameterizedType represents a parameterized type such as Collection. A parameterized type is created the first time it is needed by a reflective method, as specified in this package. When a parameterized type p is created, the generic type declaration that p instantiates is resolved, and all type arguments of p are created recursively. See TypeVariable for details on the creation process for type variables. Repeated creation of a parameterized type has no effect. Instances of classes that implement this interface must implement an equals() method that equates any two instances that share the same generic type declaration and have equal type parameters.

Method Summary

Methods inherited from interface java.lang.reflect.Type

Method Detail

getActualTypeArguments

Returns an array of Type objects representing the actual type arguments to this type. Note that in some cases, the returned array be empty. This can occur if this type represents a non-parameterized type nested within a parameterized type.

getRawType

getOwnerType

Returns a Type object representing the type that this type is a member of. For example, if this type is O.I , return a representation of O . If this type is a top-level type, null is returned.

Submit a bug or feature
For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Copyright © 1993, 2023, Oracle and/or its affiliates. All rights reserved. Use is subject to license terms. Also see the documentation redistribution policy.

Источник

Оцените статью