Understanding Common Java 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 is: Make these letters in angular brackets: <V> and <E> have special meaning. Can i use any english alphabet. that is, can they be <A> or <Q> ?

+4
source share
6 answers

They must be valid Java identifiers (not necessarily just the single letters indicated in the comment), technically you can use any identifier that you like, and your code will compile and work fine (if there are no other errors, of course!)

It is good to adhere to the convention, although these are usually single capital letters. Some of them: E , T , K and V , for an element, type, key and value, respectively - your use case may well fit into one of these categories, in which case I would use these letters. In the case of the above example, the Collection class uses E because it contains elements.

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

Defining such an interface means that you tell the compiler that you can create a link to this interface of any type ... But you only need to use letters to indicate a generic type.

Ideally, this is not a limitation 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 .. How is it ..

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

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

And whatever type you use, it will automatically affect your method return types and parameters ..

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

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

I think they should be letters. They are mainly placeholders for class names. Therefore, if you want to create an instance of MyClass, you can do

 new MyClass<String>(); 

This will replace all V in your code with String.

This tutorial contains more information 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 
0
source

Usually they do, but the compiler allows you to call them what you want; like any other type, since it is a common name for the type.

Agreement:

T is the type; most commonly used in custom classes

E is an element; used in collection

K is the key; used on the map

V is the value; used on the map

0
source

Those are E or V or something else. They simply assume that the type is unknown while writing the code. It also simplifies the job by using the same class for many types of objects.

This 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 can be defined as wildcards in java. These generics are mainly used to create a single class that is applicable to many types without requiring casting .

0
source

E , V , etc. are type parameters. They do not have to be single, lowercase letters - you can use anything that is a valid identifier in Java.

Almost everyone uses single uppercase letters for type parameters - this is an agreement that almost everyone follows, so I highly recommend that you do the same.

You can, for example, just as well:

 public class MyClass<Value> { Value v; setSomeValue(Value val) { v=val; } Value getSomeValue() { return v; } } 
0
source

Source: https://habr.com/ru/post/1437344/


All Articles