Jasper
Jasper

Reputation: 8705

Understanding Java Generic Types

Consider this Generics Code:

interface Collection<E>  {  
   public void add (E x);  
   public Iterator<E> iterator(); 
} 

And this one:

public class MyClass<V> {
    V v;
    setSomeValue(V val) {
       v=val;
    }

    V getSomeValue() {
        return v;
    }
 }

My Question: Do those letters in angular brackets: <V> and <E> have specific meaning. Can i use any English Alphabet. i.e can they be <A> or <Q>?

Upvotes: 4

Views: 652

Answers (6)

Michael Berry
Michael Berry

Reputation: 72284

They do have to be valid Java identifiers (not necessarily just single letters as pointed out in the comment,) technically you can use any identifier that you like and your code will compile and run fine (if there are no other errors of course!)

It's good to stick to convention though - this tends to be single capital letters. Some common ones are E, T, K and V, standing for element, type, key and value respectively - your use case may well fit into one of those categories, in which case I'd use those letters. In the case of the above example, the Collection class uses E because it contains elements.

Upvotes: 8

Jesper
Jesper

Reputation: 206796

The E, V etc. are type parameters. They do not have to be single, upper-case letters - you can use anything that's a valid identifier in Java.

Almost everybody uses single upper-case letters for type parameters - it's a convention that almost everybody follows, so I strongly suggest you do the same.

You could for example just as well do:

public class MyClass<Value> {
    Value v;
    setSomeValue(Value val) {
       v=val;
    }

    Value getSomeValue() {
        return v;
    }
 }

Upvotes: 0

Rohit Jain
Rohit Jain

Reputation: 213223

interface Collection<E>  {  
   public void add (E x);  
   public Iterator<E> iterator(); 
}

Defining an interface like this means that you are telling compiler that, you can create a reference of this interface of any type... But, you have to use just letters to indicate generic type..

Ideally it is not a constraint that you should use only T, E, or V.. You can use any of them.. But, your code is more readable if you follow certain conventions..

K - Key, V - Value.. Like this..

So, the references for your above interface can be: -

Collection<String>, Collection<Integer>, Collection<YourCustomObject>

And whatever type you use, that will automatically get reflected in your method return types and parameters..

So, for Collection<String>, your method would look like this: -]

   public void add (String x);  
   public Iterator<String> iterator();

Upvotes: 1

RNJ
RNJ

Reputation: 15552

I think they have to be letters. They are basically placeholders for class names. So if you wanted to instantiate MyClass you could do

new MyClass<String>();

This will replace all V in your code with String.

This lesson has more details on generics

To quote this lesson

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

Upvotes: 0

Sri Harsha Chilakapati
Sri Harsha Chilakapati

Reputation: 11940

Those E or V or whatever are just variables. They just suggest that the type is unknown at the time of writing the code. This also simplifies the job by using the same class for so many types of objects.

These can be understood from http://docs.oracle.com/javase/tutorial/java/generics/index.html

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

They are else can be defined as wildcards in java. These generics are mostly used to make a single class which is applicable for many types without requiring casting.

Upvotes: 0

Random42
Random42

Reputation: 9159

Usually they do but the compiler let's you name them as you want; as any other type since it represents a generic name for a type.

The convention is:

T - type; most used in custom classes

E - element; used in collection

K - key; used in map

V - value; used in map

Upvotes: 0

Related Questions