티스토리 뷰

back/Java

KH 18일차(Wrapper class)

devel0per 2022. 7. 25. 21:23

18일차 오늘은 Wrapper class에 대해서 학습했다.

 

Wrapper class

: 자바의 8가지 기본 자료형은 단순히 값을 저장하는 기능만 있지만 Wrapper 클래스는 다양한 기능을 사용할 수 있다.

  자바의 기본형 변수도 때로는 객체로 다루어 져야 하는 경우도 있습니다. 예를 들면 매개 변수로 객체를 요구할 때 기본형이 아닌 객체로 저장해야 할 때, 객체간의 비교가 필요할 때 등등의 경우에 기본 값들을 객체로 변환해서 작업을 수행해야 한다. 이때 사용되는 것이 Wrapper 클래스 이다. 기본 자료형은 단순히 값을 저장하는 기능만 있지만, Wrapper 클래스는 객체이므로 다양한 기능을 사용할 수 있습니다.

 

기본자료형                        Wrapper

 

 boolean                         Boolean

byte                                Byte

char                               Character

double                            Double

float                                Float

int                                   Integer

short                              Short

long                                Long

void                                Void 

 

 

public class Wrapper_1 {
	public static void main(String[] args) {
		
		Integer one = new Integer("345"); // 쓰는것을 권장하지 않음.
		Integer two = new Integer(45);
		
		int total = one.intValue() + two.intValue();
		String totalString = one.toString() + two.toString();
		
		System.out.println("one : " + one.intValue() + ", two : " + two.intValue());
		System.out.println("one : " + one + ", two " + two);
		
		System.out.println("one + two : " + total);
		System.out.println("one.toString() + two.toString() : " + totalString);
		
		System.out.println(total + " >> 2진수 : " + Integer.toBinaryString(total));
		System.out.println(total + " >> 8진수 : " + Integer.toOctalString(total));
		System.out.println(total + " >> 16진수 : " + Integer.toHexString(total));
		
		System.out.println(Integer.TYPE);
		
	}
}

Wrapper 클래스의 Integer 평소 Integer 객체는 Integer.parseInt() 메소드를 사용할 때 종종 봤다.

one이라는 이름을 가진 Integer를 객체를 생성하고 매개변수로 값을 넣으면 그 값을 Integer 객체에서 Int형(완벽하게 똑같지는 않다)으로 변환하여 가지고 있다.

intValue() 메서드를 통해 one객체와 two객체에 존재하는 매개변수를 int형으로 출력하고, 그냥 one과 two를 호출하여 출력하면 출력값은 같게 나오지만 intValue를 통한 int형과 객체 one에 존재하는 출력값은 근본이 다를것이다.

 

public class WrapperPractice {
	public static void main(String[] args) {
		
		Integer abc_1 = new Integer(789); 
		Integer abc_2 = new Integer(456);
		
		int kbs_1 = (int)abc_1; // Wrapper  class가 일반 타입으로 변환 UnBoxing;
		int kbs_2 = abc_2; // auto UnBoxing;
		
		Integer bbc_1 = (Integer)357; // 일반 타입에서 Wrapper class로 변환 Boxing;
		Integer bbc_2 = 589; // auto UnBoxing
		
		Integer king_1 = abc_1 + abc_2; // Wrapper 클래스끼리의 연산
		Integer king_2 = kbs_1 + kbs_2; // 일반 타입끼리의 연산
		
		int queen_1 = abc_1 + kbs_1;
		
		System.out.println("abc_1 : " + abc_1);  //789
		System.out.println("abc_2 : " + abc_2);  //456
		System.out.println("kbs_1 : " + kbs_1);  //789
		System.out.println("kbs_2 : " + kbs_2);  //456
		System.out.println("bbc_1 : " + bbc_1);  //357
		System.out.println("bbc_2 : " + bbc_2);  // 589
		System.out.println("king_1 : " + king_1); // 789 + 456
		System.out.println("king_2 : " + king_2); // 789 + 456
		System.out.println("queen_1 : " + queen_1); // 789 + 789

	}
}

Wrapper class 에서 일반타입으로 변환되는 것 => UnBoxing

일반타입에서 Wrapper class로 변환되는 것 => Boxing

 

public class Wrapper_4 {
	public static void main(String[] args) {
		
		int abc = new Integer("789").intValue();
		int bcd = Integer.parseInt("678"); // 기본형으로 전환
		Integer cde = Integer.valueOf("567"); // 래퍼클래스의 Integer 
		
		int int_1 = Integer.parseInt("100", 2);
		int int_2 = Integer.parseInt("100", 8);
		int int_3 = Integer.parseInt("100", 16);
		// int int_4 = Integer.parseInt("FF");
		int int_4 = Integer.parseInt("FF", 16);
		
		Integer integer_1 = Integer.valueOf("100", 2);
		Integer integer_2 = Integer.valueOf("100", 8);
		Integer integer_3 = Integer.valueOf("100", 16);
		// Integer integer_4 = Integer.valueOf("FF");
		Integer integer_4 = Integer.valueOf("FF", 16);
		
		System.out.println(abc);
		System.out.println(bcd);
		System.out.println(cde);
		
		System.out.println("100(2) : " + int_1);
		System.out.println("100(8) : " + int_2);
		System.out.println("100(16) : " + int_3);
		System.out.println("FF(16) : " + int_4);
		
		
		
		System.out.println("100(2) : " + integer_1);
		System.out.println("100(8) : " + integer_2);
		System.out.println("100(16) : " + integer_3);
		System.out.println("FF(16) : " + integer_4);
		
		
	}

}

Integer.parseInt() 와 Integer.valueOf()의 차이점은 뭘까?

간단히 설명하면 둘다 정수형을 int형태로 바꿔주는 역할을 하지만, 전자는 원시데이터로 후자는 객체로 전달해준다는 차이가 존재한다.

Java 1.5 에서 Autoboxing and Unboxing in Java 가 도입된 이후로 차이는 거의 없을 거라고 관련자료에서 말하고 있으며, 또한 이 두 메소드는 대부분의 기본 숫자 데이터타입 래퍼 클래스들인 Integer, Long, Double, Float 등과 같은 클래스 안에 포함되어 있을 거라고한다. 

 

 

 

댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
링크