Integer costs 4 times as much storage as int (16 bytes for an Integer, 4 bytes for an int).
What benefit does Integer provide?

  • A: More accuracy
  • B: Useful methods
  • C: Can use e.g. with ArrayList
  • D: All of the above
  • E: B and C only

    Answer: E

    int and Integer have exactly the same accuracy, 32 bits.

    Integer is the same as int, except that Integer comes in a really nice box. The box is so nice that it costs 3 times as much as the product inside.

    If we want to use a parameterized Java container such as ArrayList, we must use Integer: only a reference type can be used as a type parameter.

    Integer also has useful methods. Only a reference type can have methods.

    int is a low-cost, bare-bones integer, which is useful for local variables within the program and cases where there is a large array and we want to save storage.

    Prev    Next    Page+10