How to Convert Integer To Double in Java

How to Convert Integer To Double in Java | The Java programming language has two types of data types:- primitive data types and non-primitive data types. There are eight primitive data types available int, char, bool, byte, short, long, float, and double. Each primitive data types require a different amount of space when stored in the memory.

In this blog, we are converting the value of one primitive to another primitive data type, that is an int primitive is been converted to a double primitive. The difference between int and double is the double data type can have a decimal value but the integer cannot. Along with this, we will also see how to convert an Integer object to a Double object.

Convert Integer To Double in Java

Let us first see how we can convert an Integer object to a Double-object. Both Integer and Double are wrapper classes. The Integer class is used to wrap int primitive values whereas the Double class is used to wrap double primitive values.

To convert the Integer To Double we have the following ways:-

  1. Using Integer.doubleValue() method
  2. Converting using the Double.valueOf() method

1. Convert Integer To Double in Java using Integer.doubleValue()

The Java Integer class contains the doubleValue() method which converts the given Integer object to a double primitive value. Following is the method signature of the Integer.doubleValue() method:- public double doubleValue()

public class Main {
    public static void main(String args[]) {
        Integer num = 10;
        System.out.println(num);
        System.out.println(num.getClass().getName());

        Double num1 = num.doubleValue();
        System.out.println(num1);
        System.out.println(num1.getClass().getName());
    }
}

Output:-

10
java.lang.Integer
10.0
java.lang.Double

The return type of the doubleValue() method is double primitive but not the Double wrapper class type. But we can simply convert double primitive to Double wrapper class using autoboxing i.e. assign a double value to a Double-object and Java itself convert primitive into the wrapper.

public class Main {
    public static void main(String args[]) {
        Integer num = 10;

        double num1 = num.doubleValue();
        Double num2 = num1; // autoboxing
        
        System.out.println(num2);
        System.out.println(num2.getClass().getName());
    }
}

Output:-

10.0
java.lang.Double

2. Convert Integer To Double in Java using Double.valueOf()

The Java Double class contains the valueOf() method to convert the double primitive to a Double wrapper class object. Following is the method syntax of the valueOf() method:- public static Double valueOf(double d)

public class Main {
    public static void main(String args[]) {
        Integer num = 50;
        System.out.println(num);
        System.out.println(num.getClass().getName());

        Double num1 = Double.valueOf(num);
        System.out.println(num1);
        System.out.println(num1.getClass().getName());
    }
}

Output:-

50
java.lang.Integer
50.0
java.lang.Double

You might be thinking that the valueOf() method takes a double argument then how can we pass an Integer object? Actually, due to the auto-unboxing feature, the Integer object is internally converted to an int primitive value. And due to widening, we can pass int primitive value if the method is receiving double value.

Converting Int To double In Java

Now let us how can we convert int primitive value to double primitive value. To convert the int to double we use the following two ways:-

  • Converting int to double implicitly or using the assignment operator.
  • Converting using the valueOf() method.

1. How To Convert Int To double In Java using Assignment

In the below program, we are converting the int value to double by implicit conversion i.e direct assignment. Due to widening the int primitive itself converted to the double primitive value.

public class Main {
    public static void main(String args[]) {
        int num = 99;
        double num1 = num;
        System.out.println("int value: " + num);
        System.out.println("double value: " + num1);
    }
}

Output:-

int value: 99
double value: 99.0

2. How To Convert Int To double In Java using Double.valueOf() Method

Here we are converting the int to double by using the Double class valueOf() method. Following is the prototype of the valueOf() method:- public static Double valueOf(double d)

public class Main {
    public static void main(String args[]) {
        int num = 99;
        double num1 = Double.valueOf(num);
        System.out.println("int value: " + num);
        System.out.println("double value: " + num1);
    }
}

Output:-

int value: 99
double value: 99.0

The valueOf() method returns Double-object but due to the auto unboxing feature, Double-object is internally converted to a double primitive value.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *