Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
517 views
in Technique[技术] by (71.8m points)

syntax - What does the <TYPE> in java mean?

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

What does this do/mean?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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 ;)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...