Checking bounds and generic assignments

A sensor application uses the following Java class. Integer and Double extend Number, whereas String does not.


Java
final class Scalar< T extends Number > {
}


Consider each declaration independently inside a method in the same package. Enter yes if the declaration compiles or no if it does not. No casts or omitted type arguments are allowed.


Java
// Declaration A
Scalar< Integer > a = new Scalar< Integer >();

// Declaration B
Scalar< Number > b = new Scalar< Integer >();

// Declaration C
Scalar< Double > c = new Scalar< Double >();

// Declaration D
Scalar< String > d = new Scalar< String >();

// Declaration E
Scalar< Number > e = new Scalar< Number >();

// Declaration F
Scalar< Integer > f = new Scalar< Number >();
DeclarationCompiles: yes or no
A
B
C
D
E
F

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

More Generics Questions: