Binary World

JAVA CLASS : Wrapper 클래스 본문

개발자의 길/JAVA

JAVA CLASS : Wrapper 클래스

모쿠 2017. 1. 6. 17:09

<Wrapper 클래스>

* Wrapper: 자바의 기본 데이터 타입을 감싸는 클래스

    기본 자료형: boolean, byte, short, int, long, char, float, double

* Wrapper 클래스 종류:

    Boolean, Byte, Short, Integer, Long, Char, Float, Double

* wrapper 클래스를 사용하는 이유:

   1. 클래스의 메소드(기능), 상수(public static final)를 사용하기 위해서

   2. 메소드 매개변수로 Object를 갖는 메소드의 매개변수로 사용하기 위해서

   3. 컬렉션 프레임워크에서 사용하기 위해서



<프로그램 코드>


* WrapperMain01


package edu.java.wrapper01;


// Wrapper: 자바의 기본 데이터 타입을 감싸는 클래스

// 기본 자료형: boolean, byte, short, int, long, char, float, double

// wrapper 클래스:

//    Boolean, Byte, Short, Integer, Long, Char, Float, Double

// wrapper 클래스를 사용하는 이유:

// 1. 클래스의 메소드(기능), 상수(public static final)를 사용하기 위해서

// 2. 메소드 매개변수로 Object를 갖는 메소드의 매개변수로 사용하기 위해서

// 3. 컬렉션 프레임워크에서 사용하기 위해서



public class WrapperMain01 {


public static void main(String[] args) {

// Wrapper 클래스의 상수

System.out.println("byte 타입의 최소값: " + Byte.MIN_VALUE);

System.out.println("byte 타입의 최대값: " + Byte.MAX_VALUE);

System.out.println("int 최소값: " + Integer.MIN_VALUE);

System.out.println("int 최대값: " + Integer.MAX_VALUE);


System.out.println();

// wrapper 클래스의 인스턴스 생성과 사용

int n1 = 123; // 기본 자료형

Integer num1 = new Integer(123); // wrapper 클래스

System.out.println("num1 = " + num1);

// wrapper 클래스에 저장된 값을 읽어오는 메소드

int value1 = num1.intValue();

System.out.println("value1 = " + value1);

Double num2 = new Double(1.0);

System.out.println("num2 = " + num2);

double value2 = num2.doubleValue();

System.out.println("value2 = " + value2);

System.out.println("=========================");

// wrapper 클래스에 있는 static 메소드를 사용해서 인스턴스를 생성

// valueOf(), parseInt(), parseDouble(), ...

Integer num3 = Integer.valueOf(100);

System.out.println("num3 = " + num3);

Double num4 = Double.valueOf(3.14);

System.out.println("num4 = " + num4);

Integer num5 = Integer.valueOf("200");

System.out.println("num5 = " + num5);

System.out.println("=========================");

System.out.println();

// parseXxx(): 문자열을 전달 받아서, 기본 자료형을 리턴해 주는  static 메소드

int x = Integer.parseInt("123456");

System.out.println("x = " + x);

double y = Double.parseDouble("22222.3");

System.out.println("y = " + y);

} // end main()


} // end class WrapperMain01

 



* WrapperMain02


package edu.java.wrapper02;


public class WrapperMain02 {


public static void main(String[] args) {

Integer x = new Integer(100); // boxing(포장)

Integer y = new Integer(200); 

int add = x.intValue() + y.intValue(); // unboxing

System.out.println("add = " + add);

int add2 = x + y; // auto unboxing

// instance + instance 형태로 자바 5버전 이후로부터 wrapper 클래스에 한해서 가능하게 바뀌었음.

// 산술 연산자가 있으면 intvalue()를 자동으로 불러들여서 값을 넣어준다.

System.out.println("add = " + add2);

Integer z = 300; // auto boxing

System.out.println("z = " + z);

} // end main()


} // end class WrapperMain02 



* WrapperMain03


package edu.java.wrapper03;


import java.util.Scanner;


public class WrapperMain03 {


public static void main(String[] args) {

//

Scanner sc = new Scanner(System.in);

System.out.println("정수 입력>");

int n = sc.nextInt();

sc.nextLine(); // 숫자 뒤에 입력된 엔터키를 제거

System.out.println("n = " + n);

System.out.println("문자열 입력>");

String str = sc.nextLine();

System.out.println("str = " + str);

System.out.println("====================");

System.out.println("정수 입력>");

String number = sc.nextLine(); // 엔터키 제거를 해줄 필요가 없음

int x = Integer.parseInt(number); // string을 int 형태로 변환

System.out.println("x = " + x);

System.out.println("문자열 입력>");

String str2 = sc.nextLine();

System.out.println("str2 = " + str2);


sc.close();

} // end main()


} // end class WrapperMain03 




Comments