1

While learning Java Generics, I found this statement in Java Docs :

A type variable can be any non-primitive type you specify: any class type, any interface type, any array type, or even another type variable.

By array type did they mean primitive array or ArrayList ? If they mean primitive array , how should we code it ?

2
  • 2
    Any type of array can be used as a generic type parameter, e.g., List<byte[]> or Collection<String[]>. Commented Mar 20, 2017 at 6:36
  • By "any array type" they meant any array type. Commented Mar 20, 2017 at 6:38

2 Answers 2

1

Like this:

public class Test {
    static void main(String[] asdf) {
        Asdf<int[]> obj = new Asdf<int[]>();
        obj.foo();
    }
}
class Asdf<T> {
    public void foo() { }
}
Sign up to request clarification or add additional context in comments.

Comments

0

Java has a class for every array type, so there's a class for int[], there's a class for Test[]. These classes are created by JVM.

You can access them as

int[].class
Test[].class. 

Comments

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.