38

This link states the following:

The instantiation of a generic type with actual type arguments is called a parameterized type . Example (of a parameterized type):

Collection<String> coll = new LinkedList<String>();

So what is the parameterized type?

  • Collection<String> or
  • LinkedList<String>
anvarik
  • 6,417
  • 5
  • 39
  • 53
wulfgarpro
  • 6,666
  • 12
  • 69
  • 110
  • 13
    How about reading the next sentence in the link you gave: "The declaration Collection denotes a parameterized type, which is an instantiation of the generic type Collection , where the place holder E has been replaced by the concrete type String"? – JB Nizet Sep 23 '12 at 10:51
  • @JBNizet, Yes, my confusion stems from the instantiation type `LinkedList`. Your comment below clarifies it for me. – wulfgarpro Sep 23 '12 at 10:52

7 Answers7

45

They are both parameterized types: types that take other types as parameters.

The fact that you have different types on the two sides of the expression is irrelevant, and has to do with polymorphic behavior i.e. because LinkedList is a subtype of Collection.

Tudor
  • 61,523
  • 12
  • 102
  • 142
  • I think the name derives from the fact that they are types which have been *given* concrete parameters, i.e., type definitions. These parameters are what distinguish them from *generic* types. – Kallaste Apr 08 '19 at 03:59
16

Parameterized type generally is a class that deals with other object without interesting what type is it. The type may be defined using symbolic "name" and then passed when instance of class is created.

For example:

class MyClass<T> {
    private T obj;
    public MyClass<T>(T obj) {
        this.obj = obj;
    }
    public int getId() {
        return obj.hashCode();
    }
}

In example above MyClass wraps object of any type and executes its method hashCode() using the fact that this method always exists.

Here is how this class is used:

int sid = new MyClass<String>("aaaa").hashCode();

Please pay attention that you cannot say new MyClass<String>(123): the fact that object is created with parameter String dictates the type of constructor argument.

Coming back to your example Collection<String> means "collection of strings". This means that you cannot add object of other type to this collection.

Xiao
  • 12,235
  • 2
  • 29
  • 36
AlexR
  • 114,158
  • 16
  • 130
  • 208
  • if you have a where T is String, then you can pass all subclasses of T, correct? – Ungeheuer Oct 27 '16 at 04:10
  • 1
    Almost correct. Except the fact that String is final class and cannot be extended. – AlexR Oct 27 '16 at 06:03
  • So basically any generic types are Parameterized types, right? – Harvey Lin Nov 29 '16 at 19:48
  • The generic type is a type that takes other type arguments into type parameters. Generic class itself is a generic type, but generic type isn’t complete in that we cannot declare a variable generic type. Parameterized type is built from generic type when we give type arguments to generic type. So they are different. Note: raw types are also parameterized type, which is instantiated with Object type type arguments. – rosshjb Dec 09 '21 at 18:44
10

It seems generic type and parameterized type are synonymous. But it seems not right to call for example Box<Integer> a generic type since in the sense of the word not generic anymore. So probably:

A generic type is the declaration with formal type parameter/s

class Box<T> { .. }

Box<T> is a generic type

A parameterized type is the declaration with actual type argument/s

Box<Integer> b;        

Box<Integer> is a parameterized type

A raw type is the declaration of a generic type with no actual type argument/s.

class Box<T> { .. }
Box b;

Box is a raw type

Roy Alilin
  • 107
  • 1
  • 3
  • 1
    They are different. Generic types and parameterized types are not same: *A parameterized type is a class or interface type of the form C, where C is the name of a generic type and is a list of type arguments that denote a particular parameterization of the generic type.* – rosshjb Dec 09 '21 at 18:48
  • 1
    @rosshjb yes you're right they're different. But the term definitions are correct. A generic type is a class or interface that declares type parameter/s in their definition while a parameterized type is the usage of a generic type (type parameter/s are replaced with actual type argument/s, though an actual type argument type can be a type parameter). – Roy Alilin Dec 11 '21 at 15:26
  • I wrote an article about Java generic rules. It may help deepen understanding of Java generics. This is the link: https://medium.com/@royalilin/java-generics-rules-1d05de86e9cb – Roy Alilin Dec 30 '21 at 05:09
4

Here's a quick breakdown of the nomenclature...

  • Type parameter: Simply the placeholder, i.e., "<T>"
  • Type argument: Is passed to the type parameter when declaring, i.e., "<String>"
  • Parameterized type (generic): Type with formal type parameter(s), i.e., "List<T>"
  • Parameterized type (concrete): Type with declared type arguments(s) as parameters, i.e., "List<String>"
Al W
  • 89
  • 3
1

Both uses of collection

Collection<String> 
LinkedList<String>

are parameterized types here with String being the type used.

Reimeus
  • 158,255
  • 15
  • 216
  • 276
1

Even if this is a fairly old question, I still think I can add something - especially clarification to the difference between terms "generic type" and "parameterized type", which, in the beginning, confused me to some extent.

From the Java Docs Tutorial:

We call generic type to type definition, e.g.: SomeType<T>, and we call parameterized type to type invocation, e.g.: SomeType<SpecificType> (when we provide specific class as an argument to the type parameter).

E.g. List<T> is a generic type and List<String> is a parameterized type.

Giorgi Tsiklauri
  • 9,715
  • 8
  • 45
  • 66
-2

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.