Java syntax with greater than/less than: <> are they class specific?
I've been doing an Android tutorial and encountered a class with the following:
public class ImageAndTextAdapter extends ArrayAdapter<String> {
Is the <String>
a form of inheritance by type? Or is it some other Java syntax that I should know about?
The class is:
android.widget.ArrayAdapter<String>
开发者_StackOverflow
This is called generics. The class within <
and >
is a type parameter.
This is easiest explained by an example:
An ArrayList
can store items. If you specify a type parameter like this: ArrayList<String>
then this array list will store items of String
type only, (in other words, it will store String
s only)!
Similarly, the ArrayAdapter
is "parameterized" by a type as well. The ArrayAdapter
probably holds a value, and this value will be of the type specified between <
and >
, which in your case is String
.
Useful links:
- Official trail on Generics (a good starting point)
That's Java Generics.
It's like C++ templates.
It says that the ArrayAdapter is backed by an array of Strings. It's mostly useful so when you get/add objects, it's type-safe and you don't have to perform any casting.
This is part of Generics which was introduced in Java1.5, Sometimes you don't know what type of object you can pass or get, so you can pass a generic type of object "?"
. In this particular instance, you are passing ArrayAdapter object which contains String objects. you can read more http://en.wikipedia.org/wiki/Generics_in_Java
This is a Java generic class.
http://download.oracle.com/javase/tutorial/java/generics/index.html
精彩评论