그누구보다 빠르게 난남들과는 다르게 색다르게 리듬을 타는 비트 위의 나그네 🐱👤
래퍼 클래스?
boolean, char, byte, short, int, ... 같은 기본형(primitive) 변수도 객체로 다루어야 하는 경우가 있다,
매개변수로 객체를 요구할 때, 기본형 값이 아닌 객체로 저장해야할 때, 객체간의 비교가 필요할때
기본형 값들을 객체로 변환하여 작업을 수행해야 하는데 이 때 사용되는것이 래퍼(wrapper)클래스
이다.
기본형 | 래퍼클래스 | 생성자 | 활용예 |
boolean | Boolean | Boolean(boolean value) Boolean(String s) | Boolean b = new Boolean(true); Boolean b2 = new Boolean("true"); |
char | Character | Character(char value) | Character c = new Character('a'); |
byte | Byte | Byte(byte value) Byte(String s) | Byte b = new Byte((byte) 10); Byte b2 = new Byte("10"); |
short | Short | Short(short value) Short(String s) | Short s = new Short((short) 10); Short s1 = new Short("10"); |
int | Integer | Integer(int value) Integer(String s) | Integer i = new Integer(100); Integer i = new Integer("100"); |
long | Long | Long(long value) Long(String s) | Long l = new Long(100); Long l = new Long("100"); |
float | Float | Float(double value) Float(float value) Float(String value) | Float f = new Float(1.0); Float f2 = new Float(1.0f); Float f4 = new Float("1.0"); Float f3 = new Float("1.0f"); |
double | Double | Double(double value) Double(String s) | Double d = new Double(1.0); Double d2 = new Double("1.0"); |
Integer i = new Integer(100);
Integer i2 = new Integer("100");
System.out.println("i == i2 ? " + (i == i2));
System.out.println("i.equals(i2) ? " + i.equals(i2));
System.out.println("i.compareTo(i2) ? " + i.compareTo(i2));
System.out.println("i.toString() = " + i.toString());
System.out.println("MAX_VALUE = " + Integer.MAX_VALUE);
System.out.println("MIN_VALUE = " + Integer.MIN_VALUE);
System.out.println("SIZE = " + Integer.SIZE);
System.out.println("BYTES = " + Integer.BYTES);
System.out.println("TYPE = " + Integer.TYPE);
i == i2 ? false
i.equals(i2) ? true
i.compareTo(i2) ? 0
i.toString() = 100
MAX_VALUE = 2147483647
MIN_VALUE = -2147483648
SIZE = 32
BYTES = 4
TYPE = int
래퍼 클래스들은 모두 equals()가 오버라이딩 되어 있어서 주소값이 아닌 객체가 가지고 있는 값을 비교 한다.
실행결과를 보면 equals()를 이용한 두 Integer객체의 비교결과가 true이다.
그 외에도 toString, compareTo와 MAX_VALUE, MIN_VALUE, SIZE, BYTES, TYPE등
몇가지 static상수를 공통적으로 가지고 있다.
Number클래스
Number클래스
는 추상클래스로 내부적으로 숫자를 멤버변수로 갖는 래퍼 클래스의 조상이다.
위의 그림이 래퍼 클래스의 상속계층도인데 숫자와 관련된 래퍼 클래스는
모두 Number클래스
의 자손이라는 걸 확인 가능하다.
그 외에도 BigInteger
, BigDecimal
등이 있는데,BigInteger
는 long으로도 다룰 수 없는 큰 범위의 수를BigDecimal
은 double로도 다룰 수 없는 큰 범위의 부동 소수점수를
처리하기 위한 역할을 한다.
문자열을 숫자로 변환하기
문자열을 숫자로 변환하는 방법은 다양하다
int i = new Integer("100").intValue();
int i2 = Integer.parseInt("100"); // 주로 사용되는 방법..
Integer i3 = Integer.valueOf("100");
위에 코드를 보면 parseInt()
는 기본형을 반환하고 valueOf()
는 래퍼 클래스를 반환하고 있다.
문자열 → 기본형 | 문자열 → 래퍼 클래스 |
byte b = Byte.parseByte("100"); short s = Short.parseShort("100"); int i = Integer.parseInt("100"); long l = Long.parseLong("100"); float f = Float.parseFloat("3.14"); double d = Double.parseDouble("3.14"); | Byte b = Byte.valueOf("100"); Short s = Short.valueOf("100"); Integer i = Integer.valueOf("100"); Long l = Long.valueOf("100"); Float f = Float.valueOf("3.14"); Double d = Double.valueOf("3.14"); |
어차피 오토박싱(autoBoxing)
기능 때문에 반환값이 기본형일 때와 래퍼클래스일 때의 차이가 없어서 valueOf()를 쓰는것도 괜찮은 방법이지만 parse@@@()
의 성능이 조금 더 좋다.
오토박싱 & 언박싱(autoboxing & unboxing)
기본형과 참조형 간의 연산이 가능한데 이는 컴파일러가 자동으로 변환하는 코드를 넣어주는
오토박싱과 오토언박싱의 덕분이다.
컴파일 전의 코드 | 컴파일 후의 코드 |
int i = 5; Integer iObj = new Integer(7); int sum = i + iObj; | int i = 5; Integer iObj = new Integer(7); int sum = i + iObj.intValue(); |
이처럼 기본형 값을 래퍼 클래스의 객체로 자동 변환해주는 것을 `오토박싱(autoboxing)`이라 하고 반대로 래퍼 클래스 객체를 기본형으로 변환해주는 것을 언박싱(unboxing)
이라고 한다.
댓글