자격증/OCJP_ExamD

QUESTION 20

IT grow. 2018. 8. 3. 18:08
반응형


QUESTION 20
Given:
1. public class Target {
2. private int i = 0;
3. public int addOne() {
4. return ++i;
5. }
6. }
And:
1. public class Client {
2. public static void main(String[] args){
3. System.out.println(new Target().addOne());
4. }
5. }
Which change can you make to Target without affecting Client?
A. Line 4 of class Target can be changed to return i++;
B. Line 2 of class Target can be changed to private int i = 1;
C. Line 3 of class Target can be changed to private int addOne(){
D. Line 2 of class Target can be changed to private Integer i = 0;
Solution : D



plus imformation 


int VS Integer 차이점 


1. int (Primitive 자료형)

- '자료형' 을 의미한다. (int, float, long, double 와 같은 하나의 primitive 자료형을 의미합니다.)
- '산술 연산'이 가능합니다.
- null 로 초기화 불가능합니다.(0으로 초기화 가능합니다.) 
이러한 점 때문에 자바는 C/C++과 조금의 차이를 보입니다.

2. Integer (Wrapper 클래스-객체)
- Wrapper 클래스입니다.
- Unboxing 을 하지 않으면 산술 연산이 불가능하지만, null값은 처리할 수 있습니다.
- null값 처리가 용이해서 SQL 과 연동할 경우 처리가 용이. 직접적인 산술연산은 불가능합니다.

출처 : http://growinglastcanyon.tistory.com/6

반응형