简体   繁体   中英

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>

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:

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

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