본문 바로가기
카테고리 없음

[Java] 16. Wrapper class

by 슈퍼닷 2015. 2. 10.
반응형

Wrapper class 는 non - primitive type 에  primitive type을 넣을때 쓴다.

 

Object [] arr = new Object[3];

Integer wInt = new Integer(3);

arr[0] = wInt ;

 

이런 형식으로 사용한다.

그럼 다시 primitive type으로도 꺼낼수도 있지 않을까?

 

int a  = wInt.intValue();

 

intValue()를 통해 int 자료형으로 뽑아올수 있게 됬다.

 

대표적으로 쓰는 곳은 아마도 String Object 일것이다.

 


public class Test {

public static void main(String args[])
{
 String a ;
 Integer wInt = new Integer(3);
 a = wInt.toString();   // String 형태로 변환
 System.out.println(a);
}
}

 

 

String Object에 넣을라면 String Object형 자료들이어야 한다.

그래서 Wrapper class 에는 toString() 이라는 메소드가 있다.

이것을 사용하면 String 형태로 변환이 가능하다.

 

 

 Primitive Type

Wrapper class

boolean
byte
char
short
int
long
float
double

Boolean
Byte
Char
Short
Integer
Long
Float
Double

[Wrapper class 목록]

 

위는 Wrapper class 목록이다. 필요할때 참조하면 된다.

 

반응형

댓글