Learn Java by Example: Java Program to convert primitive types to objects and vice versa

Java Program to convert primitive types to objects and vice versa

In this tutorial, we will learn to convert the primitive data types to their corresponding wrapper objects and vice versa in Java.

 


Example 1: Java Program to Convert Primitive Types to Wrapper Objects


class Main{
  public static void main(String[] args){

    // create primitive types
    int var1 = 5;
    double var2 = 5.65;
    boolean var3 = true;

    //converts into wrapper objects
    Integer obj1 = Integer.valueOf(var1);
    Double obj2 = Double.valueOf(var2);
    Boolean obj3 = Boolean.valueOf(var3);

    // checks if obj are objects of
    // corresponding wrapper class
    if(obj1 instanceof Integer) {
      System.out.println("An object of Integer is created.");
    }

    if(obj2 instanceof Double) {
      System.out.println("An object of Double is created.");
    }

    if(obj3 instanceof Boolean) {
      System.out.println("An object of Boolean is created");
    }
  }
}

Output

An object of Integer is created.
An object of Double is created.
An object of Boolean is created.

In the above example, we have created variables of primitive types (intdouble, and boolean). Here, we have used the valueOf() method of the Wrapper class (IntegerDouble, and Boolean) to convert the primitive types to the objects.

 


Example 2: Java Program to Convert Wrapper Objects to Primitive Types


class Main{
  public static void main(String[] args){

    // creates objects of wrapper class
    Integer obj1 = Integer.valueOf(23);
    Double obj2 = Double.valueOf(5.55);
    Boolean obj3 = Boolean.valueOf(true);

    // converts into primitive types
    int var1 = obj1.intValue();
    double var2 = obj2.doubleValue();
    boolean var3 = obj3.booleanValue();

    // print the primitive values
    System.out.println("The value of int variable: " + var1);
    System.out.println("The value of double variable: " + var2);
    System.out.println("The value of boolean variable: " + var3);
  }
}

Output

The value of int variable: 23
The value of double variable: 5.55
The value of boolean variable: true

In the above example, we have created objects of Wrapper class (IntegerDouble, and Boolean).

We then change the objects into corresponding primitive types (intdouble, and boolean) using the intValue()doubleValue(), and booleanValue() methods respectively.

Note: The Java compiler automatically converts the primitive types into corresponding objects and vice versa. This process is known as autoboxing and unboxing.

 

 

Python Example for Beginners

Two Machine Learning Fields

There are two sides to machine learning:

  • Practical Machine Learning:This is about querying databases, cleaning data, writing scripts to transform data and gluing algorithm and libraries together and writing custom code to squeeze reliable answers from data to satisfy difficult and ill defined questions. It’s the mess of reality.
  • Theoretical Machine Learning: This is about math and abstraction and idealized scenarios and limits and beauty and informing what is possible. It is a whole lot neater and cleaner and removed from the mess of reality.

Data Science Resources: Data Science Recipes and Applied Machine Learning Recipes

Introduction to Applied Machine Learning & Data Science for Beginners, Business Analysts, Students, Researchers and Freelancers with Python & R Codes @ Western Australian Center for Applied Machine Learning & Data Science (WACAMLDS) !!!

Latest end-to-end Learn by Coding Recipes in Project-Based Learning:

Applied Statistics with R for Beginners and Business Professionals

Data Science and Machine Learning Projects in Python: Tabular Data Analytics

Data Science and Machine Learning Projects in R: Tabular Data Analytics

Python Machine Learning & Data Science Recipes: Learn by Coding

R Machine Learning & Data Science Recipes: Learn by Coding

Comparing Different Machine Learning Algorithms in Python for Classification (FREE)

Disclaimer: The information and code presented within this recipe/tutorial is only for educational and coaching purposes for beginners and developers. Anyone can practice and apply the recipe/tutorial presented here, but the reader is taking full responsibility for his/her actions. The author (content curator) of this recipe (code / program) has made every effort to ensure the accuracy of the information was correct at time of publication. The author (content curator) does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause. The information presented here could also be found in public knowledge domains.