Why is there an extra <E> in this generic method?

烈酒焚心 提交于 2020-11-30 06:38:37

问题


I learned java generics some time ago, but now I'm learning collections and found some code that I don't understand. Here is the code:

static <E> List<E> nCopies(int n, E value)

It is from class java.util.Collections.

My question is why there is:

<E> List<E>

and not only

List<E>

Obviously I am missing something, can someone clarify this for me?


回答1:


In <E> List<E>, the first <E> denotes that E is a type parameter. If you hadn't specified it, then Java would think the E in E value referred to an actual class named E, and ask you to import it. See generic methods.




回答2:


You use the <E> to typify the method you are defining.

The most common example of generics is to have a typified class like this:

public class SomeClass<E> {
    ...
}

Then, when you are creating a new object of that class you define the type directly like this:

new SomeClass<String>();

That way any method in that class that refers to <E>, will treat <E> as a String, for that instance.

Now consider a static method (which is not bound to any particular instance of a class), in order to typify that method you have use another kind of typification which applies to methods, like this:

static <E> List<E> nCopies(int n, E value)

You use the <E> before the return type to say "this particular method will consider some E when it executes". What <E> will be is decided when you invoke the method:

nCopies(3, "a");

In this example <E> will be a String, so the return type will be a List<String>.

Finally, you can even mix them both:

public class SomeClass<E> {
    public <F> void doSomething(E e, F f) {
        ...
    }
}

In this case, if you have an instance of SomeClass, the E in the doSomething method will always be String (for that instance), but the F can be anything you want it to be.




回答3:


The <E> is required to tell the compiler that you intend to use E as a type parameter, the same way you do when you make a generic class (e.g. public interface List<E>).

Since there is no rule (only conventions) on interface or class names being more than one character, and no rule (only conventions) that type parameter names have to be one character, the compiler would not know you intended it to be a type parameter rather than a concrete class name.

Edit

A lot of people have been saying this is directly related to static methods. That is not true. You can have an instance method that is generic on its own type parameters as well (though typically, the type parameters will be related to the class type parameters).

Here's an example of where you could have this:

public class MyList<E> {

    public <N super E> MyList<N> createCopy() {
        //...
    }
}

This method would allow you to create a copy of the list but not restrain you to using the same type as the list you have, but rather allowing you to use a supertype. For example:

MyList<Integer> integers = createList(1, 2, 5);
MyList<Number> numbers = integers.createCopy();



回答4:


List<E> is the return type for the method whereas <E> is the type being passed in (This is inferred by the compiler from what is being passed as E value).

static <E> List<E> someMethod(E myObject)
{
    E objectOfMyType = myObject;
    List<E> myList = new ArrayList<E>();
    ...
    return myList; 
}

This would be called as:

MyObject o = new MyObject();
List<MyObject> myList = SomeClass.someMethod(o);

IMHO the syntax for methods is kinda goofy, but there you have it. The relavent Oracle tutorial is here: http://download.oracle.com/javase/tutorial/extra/generics/methods.html




回答5:


in simple words: to indicate that E is not a class. List<E> would be a valid return type if E was a class (or interface) - despite not recommended, a class can be named with a single letter (as a type variable also could be named with more letters, even using an existing class name, to confuse anyone: static <Integer> List<Integer> method() {...}).



来源:https://stackoverflow.com/questions/8116440/why-is-there-an-extra-e-in-this-generic-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!