# Type Wrappers (Wrapper Class) ###### tags: `Java` `Basic Java` ## What is Type Wrappers? 1. A way or method to turn all the 8 primitive data types (boolean , byte , char , short , int , long , float and double) into classes. Thus, they become **reference data types**. 2. Since they are now classes, which means that we could invoke some methods written inside each classes. ## How to Wrap a Type? -- take Integer as an example 1. Manual Boxing: primitive type -> reference type * ~~public Integer (int value): through constructor~~(Removal Usage, not recommended) * **public static Integer valueOf(int i): through static method** ``` int num = 10; Integer i1 = Integer.valueOf(num); System.out.println(i1); ``` > output result: 10 2. Manual Unboxing: reference type -> primitive type ``` int i = i1.intValue(); System.out.println(i); ``` > output result: 10 3. Auto boxing & Auto Unboxing: (JDK 5 afterwards) ``` int num = 10; // Auto Boxing Integer i1 = num; // Auto Unboxing int i = i1; System.out.println(i1); System.out.println(i); ``` > output result: 10 10 ## Frequently Used Method 1. public static String toBinaryString(int i):Decimal to Binary 2. public static String toOctalString(int i): Decimal to Octal 3. public static String toHexString(int i): Decimal to Hex 4. public static int parseInt(String s): String to Integer ``` int num = 999; String s1 = Integer.toBinaryString(num); String s2 = Integer.toOctalString(num); String s3 = Integer.toHexString(num); System.out.println(s1); System.out.println(s2); System.out.println(s3); ``` >Output Result: >1111100111 >1747 >3e7 ``` String s = "123"; System.out.println(Integer.parseInt(s) + 100); ``` >Output Result: 223 (not 123100) Practice ``` String str = "10,50,30,20,40"; String[] strArray = str.split(","); int[] intArray = new int[strArray.length]; for (int i = 0; i < strArray.length; i++) { intArray[i] = Integer.parseInt(strArray[i]); } for (int i = 0; i < intArray.length; i++) { System.out.print(intArray[i] + "\t"); } ``` > 10 50 30 20 40