728x90
Wrapper Class
자료형은 크게 기본형(P타입)과 참조형(R타입)으로 나뉜다.
컬랙션에 값을 담을 때 R타입만 담을 수 있고, P타입은 컬랙션에 담을 수 없기 때문에
P타입 자료형을 R타입 자료형으로 만들어 주는 것을 랩퍼 클래스 라고 한다.
기본 자료형의 값을 멤버 변수의 값으로 저장하고
이 값 주위로 값을 가공하는 메소드 들이 감싸고 있다고 해서 랩퍼(wrapper : 감싸다) 클래스라고 불린다.
기본 자료형의 값을 컬랙션에 담기 위해서 랩퍼 클래스를 사용한다.
모든 기본 자료형은 그에 대응하는 랩퍼 클래스가 있다.
기본 자료형 (P타입) | 랩퍼 클래스 |
byte | Byte |
short | Short |
int | ※ Integer |
long | Long |
float | Float |
double | Double |
char | ※ Character |
boolean | Boolean |
예제 1.
<랩퍼클래스에 값 저장하기>
- String str = new String("값"); 과 유사한 형식으로 가능하다
Integer integer = new Integer(10);
Integer integer2 = 10;
//Integer integer3 = "10"; ---- 오류 발생
System.out.println(integer);
System.out.println(integer2);
[실행결과]
10
10
예제 2.
<R타입에서 P타입으로 변환하기>
- Value( )로 캐스팅 없이 변환 가능
integer to int
int numInt2 = integer2.intValue();
integer to short
short numShort = integer.shortValue();
예제 3.
<랩퍼클래스의 MIN_VALUE, MAX_VALUE>
System.out.println(Integer.MIN_VALUE);
System.out.println(Integer.MAX_VALUE);
[실행결과]
-2147483648
2147483647
예제 4.
<랩퍼클래스의 진법 변환>
10진법 to n진법
System.out.println(Integer.toBinaryString(3)); //2진수
System.out.println(Integer.toOctalString(9)); //8진수
System.out.println(Integer.toHexString(17)); //16진수
[실행결과]
11
11
11
n진법 to 10진법
String b = Integer.toBinaryString(3);
String o = Integer.toOctalString(9);
String h = Integer.toHexString(17);
System.out.println(Integer.parseInt(b, 2));
System.out.println(Integer.parseInt(o, 8));
System.out.println(Integer.parseInt(h, 16));
[실행결과]
3
9
17
※ n진법 숫자 표현
2진법 0b__
int bi = 0b11; //2+1 = 3
8진법 0__
int oi = 011; //8+1 = 9
16진법 0x__
int hi = 0x11; //16+1 = 17
※※ n진법으로 값을 저장해도 십진법으로 반환된다.
int bi = 0b11;
int oi = 011;
int hi = 0x11;
System.out.println(bi);
System.out.println(oi);
System.out.println(hi);
[실행결과]
3
9
17
예제 5.
<Integer - Integer 값 비교하기>
- == : 같은 객체이면 true, 아니면 false
- .equals( ) : 같은 값을 가지면 true, 아니면 false
- a.compareTo(b) :
a == b : 0
a > b : +1
a < b : -1
integer = 200; //integer : Integer타입 200
integer2 = new Integer(200); //integer2 : Integer타입 200
System.out.println(integer == integer2); //같은 객체가 아니기 때문에 false
System.out.println(integer.equals(integer2)); //같은 값이기 때문에 true
System.out.println(integer.compareTo(integer2)); //같은 값이기 때문에 0
[실행결과]
false
true
0
예제 6.
<int - Integer 값 비교하기>
numInt = 200; //int
integer = 200; //Integer
if(integer.equals(numInt)) {
//.equlals는 입력 값이 Integer여야 하기 때문에
//컴파일러가 자동으로 numInt를 new Integer(numInt)로 변경하여 연산
System.out.println("같아요.");
}else {
System.out.println("달라요.");
}
[실행결과]
같아요.
예제 7.
<오토 박싱과 오토 언박싱>
오토 박싱 R = P
Integer integer = 200;
int numInt = 200;
integer = numInt;
오토 언박싱 P = R
int numInt = 200;
Integer integer = 200;
numInt = integer;
- integer 객체 안의 int값이 나와서 numInt에 대입된다.
예제 8.
<HashSet에 넣기>
double pi = 3.14;
Double rPi = pi; //R타입으로 변경
rPi = 3.14;
Set<Double> cSET = new HashSet<Double>();
cSET.add(rPI); //R
cSET.add(3.14); //p
예제 9.
<ArrayList에 넣기>
ArrayList<Integer> aar = new ArrayList<Integer>();
aar.add(integer); //R
aar.add(integer2 + 10); //R
aar.add(123); //p
예제 10.
<Object 객체 연산>
- Object를 이용하여 다양한 방법으로 계산하기
- Object obj = 10
Object obj = 10;
int num = (int)obj + 1;
System.out.println(num);
[실행결과]
11
Object obj = 10;
obj = "10"; //obj의 자료형은 'Object'이고, 'String'이 아니다.
System.out.println(obj);
int num = Integer.valueOf((String)obj) + 1; //Object -> String으로 캐스팅
System.out.println(num5);
[실행결과]
10
11
Object obj = 10;
int num = ((Integer)obj).intValue() + 1;
System.out.println(num);
[실행결과]
11
'JAVA' 카테고리의 다른 글
컬렉션 (Collection) (0) | 2021.07.20 |
---|---|
제네릭 (Generic) (0) | 2021.07.20 |
메모리 (Memory) (0) | 2021.07.19 |
Enum (0) | 2021.07.19 |
다형성 (Polymorphism) (0) | 2021.07.19 |