Java syntax with greater than/less than: <> are they class specific?

前端 未结 4 2004
萌比男神i
萌比男神i 2021-01-01 18:20

I\'ve been doing an Android tutorial and encountered a class with the following:

public class ImageAndTextAdapter extends ArrayAdapter {


        
相关标签:
4条回答
  • 2021-01-01 18:35

    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.

    0 讨论(0)
  • 2021-01-01 18:37

    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 Strings 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)
    0 讨论(0)
  • 2021-01-01 18:42

    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

    0 讨论(0)
  • 2021-01-01 18:54

    This is a Java generic class.

    http://download.oracle.com/javase/tutorial/java/generics/index.html

    0 讨论(0)
提交回复
热议问题