5월, 2020의 게시물 표시

[ocjap] List Null Unboxing

Which of the following are true statements about the following code? (Choose all the apply) import java.util.ArrayList; import java.util.List; public class ListNull { public static void main(String[] args) { 4: List<Integer> ages=new ArrayList<>(); 5: ages.add(Integer.parseInt("5")); 6: ages.add(Integer.valueOf("6")); 7: ages.add(7); 8: ages.add(null); 9: for(int age:ages) System.out.print(age); } } A) Exactly three of the add statements uses autoboxing. B) Exactly two of the add statements uses autoboxing. C) The code compiles. D) The code throws a runtime exception. E) Exactly one of the add statements uses autoboxing. 5,7 라인은 int를 Integer로 바뀌는 오토박싱을 사용한다. 6라인은 그러지 않는데 이유는 valueOf() 는 Integer를 리턴하기 때문이다. for loop는 null을 int로 언박싱을 하려고 할때 NullPointerExceptiion runtime exception을 발생시킨다. 정답: B,C,D 참고 int java.lang.Integer.parseInt(String s) throws NumberFormatException Integer java.lang.Integer.valueOf(String s) throws NumberFormatExc...