简体   繁体   中英

What does the <TYPE> in java mean?

I have seen declarations, interfaces and classes that go TYPE<CLASS>

What does this do/mean?

Without evidence, I believe you're talking about Java's Generics support...

Generics allow you to abstract over types

Before Java 5 it was difficult to provide classes that were capable of supporting multiple different types of Objects without having to code for each specific situation, so it was common for people to pass Object instead.

This leads to many difficult choices to make at runtime, you'd have to do a runtime check to see if it was possible to cast a given Object to a usable type...for example

List myIntList = new LinkedList(); // 1
myIntList.add(new Integer(0)); // 2
Integer x = (Integer) myIntList.iterator().next(); // 3    

Now, this is reasonably obvious, but if you were passed just a List , you'd have to check each and every element in the list for correctness...

But now, we can do this...

List<Integer> myIntList = new LinkedList<Integer>(); // 1'
myIntList.add(new Integer(0)); // 2'
Integer x = myIntList.iterator().next(); // 3'

This is a contract that basically says "This list only contains Integer type's of objects".

With generics you can construct a single class that is capable of handling multiple different data types or a family of data types (ie constraint the parameter so that it must be extended from a particular parent type).

Iterator<? extends Number> itNum;

Basically says, this will contain objects that inherit from Number , include Integer , Long , Double , Float ...

Often in method and class decelerations you will see something similar to...

public class MyGenericClass<T> {...}

or

public class MyGenericClass<T extends MyBaseObject> {...}

This allows you to refer to T as if it were a concrete object type, for example...

public class MyGenericClass<T extends MyBaseObject> {
    private T value;
    public MyGenericClass(T value) {
        this.value = value;
    }
}

This allows the compiler (and JVM) to essentially "replace" the marker T with a concert type (okay, it's a little more complicated then that, but that's the magic)...

This allows to do things like...

... new MyGenericClass<MySuperObject>(new MySuperObject());
... new MyGenericClass<MySuperSuperObject>(new MySuperSuperObject());

And know that it will only ever accept the type of object I specify...

Have a read through the link in the first paragraph, I'm sure it can do more justice then I can ;)

public class Grid<E> {

That's how you define a generic class in Java. Grid is the class and E is a formal type parameter.

If you are really interested in learning about it, you will find a very good reference here - Java Generics FAQs - Frequently Asked Questions

that is generic types check it here . Simple examples would be

List<String>

Map<Integer, String>

It's unclear what you are asking without looking at what exactly you are seeing. But it's likely you are seeing Generics in Java. Learn more about it here

The idea is basically to make stronger type-safety in Java. So, a declaration like List<Integer> intList means intList has Integers in it. And if you try to put a, say, String -- it will throw compilation error.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM