What is the primary purpose of Wrapper classes in Java?
Wrapper classes increase the speed of programs by optimizing primitive operations.
Incorrect: Wrapper classes result in slower speeds due to the additional overhead from managing Objects instead of primitives and boxing/unboxing.
Wrapper classes turn primitive data types into Objects.
Correct!
Wrapper classes increase the precision of mathematical calculations.
Incorrect: Wrapper classes do not provide any increased precision in calculations. The precision of a calculation depends on the type (ex. float is more precise than an int), not by whether the data is in its primitive form or Object/Wrapper class form.
2.Wrapper Classes: Memory.
Using wrapper classes in Java reduces memory usage compared to using primitive data types.
True.
Using wrapper classes in Java increases memory usage because they are objects (which come with additional overhead).
False.
Using wrapper classes in Java increases memory usage because they are objects (which come with additional overhead).
3.Wrapper Classes: Stack vs. Heap.
Consider the following lines of code:
int primitiveInt = 50;
Integer wrapperInt = new Integer(50);
Which of the following statements describes where primitiveInt and wrapperInt are stored?
primitiveInt and wrapperInt are stored on the stack.
Incorrect: wrapperInt is an Object from the Integer class and objects in Java are stored on the heap, not the stack.
primitiveInt and wrapperInt are stored on the heap.
Incorrect: In Java, primitives are stored on the stack.
primitiveInt is stored on the heap and wrapperInt is stored on the stack.
Incorrect: Primitives in Java are stored on the stack and wrapperInt, which is an object of the Integer class, is stored on the heap.
primitiveInt is stored on the stack and wrapperInt is stored on the heap.
Correct: Primitive data types are stored on the stack and objects are stored on the heap in Java.
4.Wrapper Classes: int[] vs ArrayList of Integer.
Which of the following statements best describes the tradeoff between using an int[] over an ArrayList of Integer?
Both int[] and an ArrayList of Integer are equally preferable in all scenarios because they perform identically in terms of memory usage and speed.
Incorrect: int[] are faster for basic operations (accessing/updating elements) and are more memory efficient. The dynamic resizing capability of an ArrayList contributes to additional memory overhead.
An ArrayList of Integer is slower than an int[] because of its internal complexity.
Correct: The flexibility of an ArrayList of Integer comes with a performance cost due to its internal complexity, such as resizing and handling Objects instead of primitives.
int[] is slower than an ArrayList of Integer because it does not have the capability to store data efficiently.
Incorrect: int[] is faster for operations such as accessing and setting elements because it stores values without the overhead of dynamic resizing.
The choice between using an int[] and an ArrayList of Integer is purely for style purposes.
Incorrect: Depending on whether you choose between an int[] and an ArrayList of Integer is based on the needs of the program, such as performance, memory efficiency, and the required functionality.