Return to site

JAVA CERTIFICATION QUESTION: Generics and collections

October 8, 2022

Given:

List/* Spot 1 */ list = new ArrayList/* Spot 2 */();

At which position can the diamond operator '<>' be inserted without throwing a compilation error?

  • A. None of Spot 1 or Spot 2
  • B. Spot 1 only
  • C. Spot 2 only
  • D. Either Spot 1 or Spot 2, but not both
  • E. Both Spot 1 and Spot 2 at the same time

#java #certificationquestion #ocp

 

 

From Oracle Java trails: https://bit.ly/3M1aCLM

You can replace the type arguments required to invoke the constructor of a generic class

with an empty set of type arguments (<>) as long as the compiler can determine, or infer, the type arguments from the context.

This pair of angle brackets, <>, is informally called the diamond.

For example, you can create an instance of Box with the following statement:

Box integerBox = new Box<>();

·ꓛ sᴉ ɹǝʍsuɐ ǝɥꓕ :uoᴉsnʅɔɯoꓛ