자바 Wrapper 클래스, 박싱, 언박싱, 자동박싱, 자동언박싱
package test;
public class Test2 {
public static void main(String[] args) {
// Wrapper : 자바는 기본적으로는 객체지향언어
// 객체를 대상으로 처리하는 경우가 많음
// 기본자료형을 객체로 변환해서 사용하도록 Wrapper 클래스 제공
System.out.println(Integer.MIN_VALUE);
System.out.println(Integer.MAX_VALUE);
Integer i1 = new Integer(10); // 현재 deprecated 되었지만, 사용할수는 있다.
Integer i2 = new Integer("20");
i1 = 10;
i2 = 20;
// 문자열 -> 기본자료형 바꾸기
int a = Integer.parseInt("30");
System.out.println(a);
// 문자열 -> Integer 클래스형
Integer b = Integer.valueOf("40");
String str = "3.14f";
float f = Float.parseFloat(str);
System.out.println(f);
System.out.println("============");
Integer n = new Integer(200); // 박싱(기본자료형->Wrapper클래스)
int e = n.intValue(); // 언박싱(Wrapper클래스형->기본자료형)
// JDK 1.5부터 자동박싱, 자동언박싱 지원
Integer n1 = 200; // 자동박싱
Integer n2 = 100;
System.out.println(n1 + n2); // 자동언박싱
// System.out.println(n1.intValue() + n2.intValue());
// int num = null; // 기본자료형은 null 저장안됨
Integer num2 = null;
Boolean bVal = true; // 자동박싱
System.out.println(bVal && true); // 자동언박싱
Boolean bVal2 = Boolean.valueOf(true); // 박싱
System.out.println(bVal2.booleanValue() && true); // 언박싱
Float fVal = Float.valueOf(23.3f); // 박싱
float f2 = fVal.floatValue(); // 언박싱
Float fVal3 = 3.14f; // 자동박싱
float f3 = fVal3; // 자동언박싱
// 기본자료형 -> 문자열 변환
String strNum = 20 + "";
strNum = Integer.toString(20);
} // main()의 끝
}