Java provides a mechanism to convert between primitive data types and their corresponding wrapper classes. This conversion is categorized into two processes:
- Autoboxing: Converting a primitive type to its wrapper class object.
- Unboxing: Converting a wrapper class object to its primitive type.
Conversion Methods


Example Program: Demonstrating Conversion Between Wrappers and Primitives
public class ConversionDemo { public static void main(String[] args) { // Autoboxing: Primitive to Wrapper Integer intObj = Integer.valueOf(10); Double doubleObj = Double.valueOf(3.14); Boolean boolObj = Boolean.valueOf(true); System.out.println("Integer Object: " + intObj); System.out.println("Double Object: " + doubleObj); System.out.println("Boolean Object: " + boolObj); // Unboxing: Wrapper to Primitive int intPrimitive = intObj.intValue(); double doublePrimitive = doubleObj.doubleValue(); boolean boolPrimitive = boolObj.booleanValue(); System.out.println("Primitive int: " + intPrimitive); System.out.println("Primitive double: " + doublePrimitive); System.out.println("Primitive boolean: " + boolPrimitive); // String to Wrapper Integer strToInt = Integer.valueOf("100"); Double strToDouble = Double.valueOf("99.99"); System.out.println("String to Integer: " + strToInt); System.out.println("String to Double: " + strToDouble); // Wrapper to String String intStr = intObj.toString(); String doubleStr = doubleObj.toString(); System.out.println("Integer to String: " + intStr); System.out.println("Double to String: " + doubleStr); // Parsing String to Primitive int parsedInt = Integer.parseInt("123"); double parsedDouble = Double.parseDouble("45.67"); System.out.println("Parsed int: " + parsedInt); System.out.println("Parsed double: " + parsedDouble); } } /* Integer Object: 10 Double Object: 3.14 Boolean Object: true Primitive int: 10 Primitive double: 3.14 Primitive boolean: true String to Integer: 100 String to Double: 99.99 Integer to String: 10 Double to String: 3.14 Parsed int: 123 Parsed double: 45.67 */
Conversion between wrappers and primitives is crucial for utilizing Java’s data structures and performing data manipulation. Autoboxing and unboxing simplify conversions, while methods like valueOf()
, parseInt()
, and toString()
offer explicit control over data conversion.