0

Hi I came across a code in documentation of android in Grid view for the following code.

gridview.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
            Toast.makeText(HelloGridView.this, "" + position, Toast.LENGTH_SHORT).show();
        }
    });

In this what does the < ?> (space added as stackoverflow didn't allow without space) indicate/do?

1
  • 1
    This has nothing to do with Android. But you should add the generics tag Commented Jul 9, 2011 at 13:56

1 Answer 1

3

The < ? > is a wild card for the generic type, meaning the generic type for AdapterView can be anything at all.

More specifically in this case the parameter on the method may receive an AdapterView with absolutely any generic type. As a note if you wanted to limit the generic type you could do:

AdapterView<? extends myClass)

This limits the generic type to myClass or anything that extends myClass.

Just as a note:

 AdapterView <?> and AdapterView<? extends Object> 

Are identical.

You can find additional information here in the wildcards section

Java generics documentation

Sign up to request clarification or add additional context in comments.

2 Comments

So basically its used when we dont want to specify the type of the object(where required like in a Collection or Vector) and should we want it to be general case method?
@Footy Yes it is used when you want to allow your parameter to be of any generic type without limiting it at all. If you wanted to limit it you can use <? extends myClass>, then it would only accept generics that extends or that are myClass.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.