Generic type in AP Computer Science A

A generic type in AP Computer Science A is a class like ArrayList<E> whose angle-bracket type parameter E specifies the type of elements it holds, so methods like add() and get() accept and return that exact type and the compiler can catch type mismatches before the program runs.

Verified for the 2027 AP Computer Science A examLast updated June 2026

What is generic type?

A generic type is a Java class with a type parameter in angle brackets. On the AP exam, that means ArrayList<E>, where E is a placeholder you fill in when you create the list. Writing ArrayList<String> names = new ArrayList<String>(); tells Java that this list holds Strings and nothing else. From that point on, every ArrayList method is locked to that type. add() only accepts Strings, and get() hands back a String you can use immediately, no casting required.

The payoff is compile-time type safety. Per EK 4.8.A.3, ArrayList<E> is preferred over plain ArrayList because the compiler can find type errors that would otherwise only show up as crashes at run-time. Think of the angle brackets as a label on a storage bin. Without the label, you find out something wrong is in the bin only when you reach in and get surprised. With the label, Java refuses to let the wrong item in at all.

Why generic type matters in AP® Computer Science A

Generic types live in Topic 4.8 (ArrayList Methods) in Unit 4: Data Collections, supporting learning objective AP Comp Sci A 4.8.A, which asks you to develop code using ArrayList objects and determine the result of calling their methods. You can't do either of those without reading the generic type. The <E> tells you what get() returns, what add() accepts, and whether a code segment even compiles. Since Unit 4 carries heavy weight on both the multiple-choice section and the array/ArrayList FRQ, the ArrayList<Type> declaration is something you'll write or interpret on nearly every collections question.

How generic type connects across the course

.get() method (Unit 4)

The generic type is what makes get() useful. If the list is declared ArrayList, then list.get(0) returns a String you can call String methods on directly. With a raw ArrayList, you'd just get a generic Object back.

Object reference (Unit 4)

Per EK 4.8.A.1, an ArrayList stores object references, not the objects themselves. The generic type tells you what kind of object those references point to. This is also why you can't write ArrayList; the type parameter must be a class like Integer, since primitives aren't objects.

ArrayList constructor (Unit 4)

EK 4.8.A.2's constructor pairs with the generic type in one line. The declaration ArrayList names = new ArrayList(); builds an empty list that the compiler treats as String-only from the very first add() call.

Is generic type on the AP® Computer Science A exam?

Generic types show up the moment any question instantiates an ArrayList. A common multiple-choice setup gives you a code segment with /* missing code */ and asks which declaration correctly creates the list, for example picking ArrayList<String> list = new ArrayList<String>(); over options with wrong syntax or mismatched types. You also need to trace what get() returns based on the declared type parameter. On the free-response section, the array/ArrayList question expects you to declare lists with the correct generic type yourself. Writing a raw ArrayList instead of ArrayList<E> is exactly the kind of thing the CED says to avoid, so always include the angle brackets.

Generic type vs Raw ArrayList (no type parameter)

A raw ArrayList compiles, but it treats every element as a plain Object, so type mistakes only blow up at run-time. ArrayList moves that error to compile-time. EK 4.8.A.3 is explicit that ArrayList is preferred. Same class, but the angle brackets are the difference between the compiler protecting you and a surprise crash.

Key things to remember about generic type

  • A generic type uses angle brackets, like ArrayList, to specify exactly what type of elements a collection holds.

  • When you declare ArrayList, the ArrayList methods' parameter types and return types all become type E, so get() returns that type with no casting.

  • ArrayList is preferred over raw ArrayList because the compiler catches type errors that would otherwise crash the program at run-time.

  • The type parameter must be a class, not a primitive, because an ArrayList stores object references (so use Integer, not int).

  • The standard exam-ready declaration is ArrayList name = new ArrayList(); and you should write it this way on the FRQs.

Frequently asked questions about generic type

What is a generic type in AP Computer Science A?

It's a class with a type parameter in angle brackets, like ArrayList, where E names the element type. Declaring ArrayList means the list only holds Strings, and methods like add() and get() work with Strings specifically.

Can you use int as a generic type, like ArrayList<int>?

No. ArrayLists store object references, and int is a primitive, not an object. Use the Integer wrapper class instead, as in ArrayList.

Is ArrayList without angle brackets wrong on the AP exam?

It compiles, but the CED (EK 4.8.A.3) says ArrayList is preferred because the compiler can catch type errors that a raw ArrayList would only reveal at run-time. Always write the angle brackets on FRQs.

What's the difference between ArrayList<E> and ArrayList<String>?

ArrayList is the general form, where E is a placeholder type parameter. ArrayList fills in that placeholder with an actual class, creating a list that holds only Strings. You write the filled-in version in real code.

Why do generics matter for the get() method?

The generic type sets get()'s return type. With ArrayList, list.get(0) returns a String you can use directly, like calling .length() on it. Without the type parameter, get() returns Object and you lose that compile-time guarantee.

Generic Type — AP Comp Sci A Definition & Exam Guide | Fiveable