Java Program To Convert Primitive Data Types To Objects

In Java, you can convert primitive types to their corresponding wrapper class objects (also known as autoboxing) and vice versa (unboxing) using several different methods.

Here is an example of converting a primitive type int to its corresponding wrapper class object Integer.


int i = 42;
Integer i2 = Integer.valueOf(i);

In this example, the Integer.valueOf() method is used to convert the primitive type int to its corresponding wrapper class object Integer.
Here is an example of converting a wrapper class object Integer to its corresponding primitive type int.


Integer i2 = 42;
int i = i2.intValue();

In this example, the intValue() method is used to convert the wrapper class object Integer to its corresponding primitive type int.

You can use similar methods to convert other primitive types to their corresponding wrapper class objects and vice versa. Here are some examples:

double d = 12.5;
Double d2 = Double.valueOf(d);

Double d2 = 12.5;
double d = d2.doubleValue();

boolean b = true;
Boolean b2 = Boolean.valueOf(b);

Boolean b2 = true;
boolean b = b2.booleanValue();

char c = 'A';
Character c2 = Character.value

Java Program To Convert Primitive Data Types To Objects

Here is a full Java program that demonstrates the use of autoboxing and unboxing for different primitive types

public class PrimitiveToObject {
  public static void main(String[] args) {
    // Autoboxing - int to Integer
    int i = 42;
    Integer i2 = Integer.valueOf(i);
    System.out.println("Autoboxing int to Integer: " + i2);

    // Unboxing - Integer to int
    Integer i3 = 42;
    int i4 = i3.intValue();
    System.out.println("Unboxing Integer to int: " + i4);

    // Autoboxing - double to Double
    double d = 12.5;
    Double d2 = Double.valueOf(d);
    System.out.println("Autoboxing double to Double: " + d2);

    // Unboxing - Double to double
    Double d3 = 12.5;
    double d4 = d3.doubleValue();
    System.out.println("Unboxing Double to double: " + d4);

    // Autoboxing - boolean to Boolean
    boolean b = true;
    Boolean b2 = Boolean.valueOf(b);
    System.out.println("Autoboxing boolean to Boolean: " + b2);

    // Unboxing - Boolean to boolean
    Boolean b3 = true;
    boolean b4 = b3.booleanValue();
    System.out.println("Unboxing Boolean to boolean: " + b4);

    // Autoboxing - char to Character
    char c = 'A';
    Character c2 = Character.valueOf(c);
    System.out.println("Autoboxing char to Character: " + c2);

    // Unboxing - Character to char
    Character c3 = 'A';
    char c4 = c3.charValue();
    System.out.println("Unboxing Character to char: " + c4);
  }
}
Output
Autoboxing int to Integer: 42
Unboxing Integer to int: 42
Autoboxing double to Double: 12.5
Unboxing Double to double: 12.5
Autoboxing boolean to Boolean: true
Unboxing Boolean to boolean: true
Autoboxing char to Character: A
Unboxing Character to char: A

This program demonstrates the use of autoboxing and unboxing for `int`, `double`, `boolean`, and `char` primitive types.

It's important to note that the wrapper classes `Integer`, `Double`, `Boolean`, `Character` have also the following methods parseInt, parseDouble, parseBoolean, parseChar which can also be used to convert string to respective wrapper classes.