Briefly describe what autoboxing is inJava. Write code that would create an Arra
ID: 3613794 • Letter: B
Question
- Briefly describe what autoboxing is inJava.
- Write code that would create an ArrayList that can store typeint. The list should be stored in a reference called "nums". Notethat you cannot make an ArrayList that directly stores a primitivetype.
- Write a line of code that would create an array (not ArrayList)of 20 integers called "data".
- Given an array of doubles called "values", write a loop thatwould set each element to 1.0. Hint: you'll need to use a regular"for" loop and figure out how to find out how many elements thereare in the list.
Explanation / Answer
Briefly describe whatautoboxing is in Java. Autoboxing occurs when the compiler convertsprimitive data types to objects, when an object is required. For example, suppose you have a method publicvoid fun(Object o) { System.out.println(o.toString()); } Now, because int is a primitive datatype, the method cannot be passed an int. However, the following code is legalbecause of autoboxing: int i = 2; fun(2); The compiler turns that primitive data type intoan object. Essentially what happens is the following: int i = 2; fun(new Integer(2)); ____________________ Write aline of code that would create an array (not ArrayList) of 20integers called "data". Integer[]array = new Integer[20]; ____________________ Givenan array of doubles called "values", write a loop that would seteach element to 1.0. Hint: you'll need to use a regular "for" loopand figure out how to find out how many elements there are in thelist. double[]values; for(int i= 0; iRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.