본문 바로가기
자바(Java)/자바 AP

AP Computer Science A Java Quick Reference (2) Integer & Double

by 긱펀 2024. 3. 28.
반응형

AP Computer Science A Java Quick Reference 

(2) Integer & Double

 

 

 

1.Wrapper Class - Integer & Double

1
2
3
4
5
6
7
// in older versions of Java (and on the AP exam)
Integer i = new Integer(2); // create an object with 2 in it
Double d = new Double(3.5); // create an object with 3.5 in it
 
// in newer versions of Java (9+)
Integer i = 2;
Double d = 3.5;
cs

 

  • wrapper class for int is Integer.
  • wrapper class for double is Double.
  • int, double의 primitive type 대신에 Integer, Double 같은 wrapper 클래스를 만들면 다양한 method를 사용할 수 있는 장점이 있습니다.
  • Java 9 버전이후로는 그냥 = 연산을 사용하여 값을 바로 대입해 Integer, Double 오브젝트를 만들어도 됩니다.
  • 생성자(Constructor)를 사용하여 Integer, Double 오브젝트를 만드는 것은 예전 버전의 Java방식이고, APCSA시험에서는 예전 버전의 Java까지 범위를 포함하므로 생성자 방식이 나올 수 있습니다.

 

[예제1]

아래 코드를 실행해보세요. 만약 Integer 최대, 최소값의 범위를 넘어서면 어떻게 되는지 확인해 보세요.

1
2
3
4
5
6
7
8
public class Main {
    public static void main(String[] args) {
        System.out.println(Integer.MIN_VALUE);        // -2147483648
        System.out.println(Integer.MAX_VALUE);        // 2147483647
        System.out.println(Integer.MIN_VALUE - 1);    // 2147483647
        System.out.println(Integer.MAX_VALUE + 1);    // -2147483648
    }
}
cs

 

  • 자바에서 int값의 범위는 -2147483648 ~ 2147483647 입니다.
  • overflow : 최대값보다 +1 만큼 넘어설 경우 자바에서는 최소값을 리턴한다.
  • underflow : 최소값보다 -1 만큼 넘어설 경우 자바에서는 최대값을 리턴한다.

 

 

[예제2]

아래코드를 시행해보고 Autoboxing 과 Unboxing에 대해서 알아보자.

1
2
3
4
5
6
7
// Autoboxing
Integer i = 2;
Double d = 3.5;
 
// Unboxing
Integer i = 2;  // autoboxing - wrap 2
int number = i; // unboxing - back to primitive type
cs

 

  • Autoboxing : primitive type을 wrapper class로 자동 변환하는 것(int to Integer, double to Double)
  • Unboxing : wrapper class를 primitive type으로 자동 변환하는 것(Integer to int, Double to double)
  • 자바에서는 Autoboxing이나 Unboxing이 위의 코드와 같이 = 연산으로 할 때도 작동되고, 메소드의 매개변수를 통해서도 자동으로 작동 됩니다.

 

 

[예제3] 아래 예제코드를 실행해 보세요.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class Main {
    public static void main(String[] args) {
        Integer i = 2;
        Double d = 3.5;
        // intValue(),doubleValue() returns the primitive value
        System.out.println(i.intValue()); 
        System.out.println(d.doubleValue());
 
        String ageStr = "16";
        // Integer.parseInt and Double.parseDouble are often used to
        // convert an input string to a number so you can do math on it.
        // They are not on the AP exam
        System.out.println("Age " + ageStr + " in 10 years is "
                        + (Integer.parseInt(ageStr) + 10));
        System.out.println("Note that + with strings does concatenation, not addition: " + (ageStr + 10));
    }
}
cs

 

  • intValue() : Returns the value of this Integer as an int.
  • doubleValue() : Returns the value of this Double as a double.

 

 

[유제1] 아래 코드에서 잘못된 부분을 고쳐서 다시 코딩해 보세요.

1
2
3
4
5
6
7
8
9
10
11
public class Main {
    public static void main(String[] args) {
        integer i = 2.3;
        Double d = 5;
        System.out.println( i.intValue );
        System.out.println( doubleValue() );
        // Print out the min and max values possible for integers
        System.out.println(Integer.min_value);
        System.out.printlnint.MAX_VALUE() );
    }
}
cs

 

더보기

[유제1 정답]

1
2
3
4
5
6
7
8
9
10
11
public class Main {
    public static void main(String[] args) {
        Integer i = 2;
        Double d = 5.0;
        System.out.println( i.intValue() );
        System.out.println( d.doubleValue() );
        // Print out the min and max values possible for integers
        System.out.println(Integer.MIN_VALUE);
        System.out.println(Integer.MAX_VALUE );
    }
}
cs

 

<요약>

  • Integer, Double 클래스는 primitive types(int, double)으로부터 object를 만들 수 있는 wrapper class이다.
  • wrapper class를 사용하면 아래와 같은 메소드를 사용할 수 있는 장점이 있다.
  • Integer(value): Constructs a new Integer object that represents the specified int value.
  • Double(double value) : Constructs a new Double object that represents the specified double value.
  • Integer.MIN_VALUE : The minimum value represented by an int or Integer.
  • Integer.MAX_VALUE : The maximum value represented by an int or Integer.
  • int intValue() : Returns the value of this Integer as an int.
  • double doubleValue() : Returns the value of this Double as a double.

 

  • Autoboxing is the automatic converting an int to an Integer and a double to a Double.
  • Unboxing is the automatic converting an Integer to an int and a Double to a double.

 


 

728x90
반응형

댓글