Convert Int To Double In Java

In this tutorial we will learn different method for convert int to double in java .  As we know , Double data types has longer range than int data type in java . So , Java implicitly converts int value to double value without the need of typecasting.Let’s discuss how to convert int to double in java using below methods .

  1. Convert int to double using Double.valueOf() method
  2. Convert int to double using Double wrapper class constructor
  3. Java Convert Int to Double using Integer.doubleValue()
  4. Convert Int to Double using assignment operator – Widening Casting

1.  Convert int to double using Double.valueOf() method

Java Double wrapper class has valueOf() method which is used to converting int to double. In this example , we will take a int variable num and convert int(num varaible) to double using valueOf() of Double class. Let’s see code of java convert int to double using Double.valueOf() .

public class IntToDoubleJava {
	
	public static void main(String[] args) {
	        // initialize  int variable 
	        int num = 99;
	     
	        // converting int to double
	        // using Double valueOf() 
	        Double doubleValue = Double.valueOf(num);
	     
	        //print the double value
	        System.out.println("doubleValue : "+doubleValue);
	}

}

Output:

doubleValue : 99.0

2. Convert int to double using Double wrapper class constructor

We can convert int to double in java using Double class constructor . Double class have parameter constructor , In this example, we will create double object using constructor from int value .

public class IntToDoubleJava1 {

	public static void main(String[] args) {
		//  initialize  int variable 
        int num = 99;
     
        // converting int to double
        // using Double wrapper class
        //constructor 
        Double doubleValue = new Double(num);
     
        //print the double value
        System.out.println("doubleValue :"+doubleValue);
	}
	
}

Output:

doubleValue :99.0

3. Java Convert Int to Double using Integer.doubleValue()

We can convert int to double in java using Integer.doubleValue() . It returns the value of this Integer object as a double after a widening primitive conversion. Let’s see casting int to double in java using doubleValue() method of Integer class .

public class IntToDoubleJava2 {
      public static void main(String[] args) {
    	//  initialize  int variable 
          int num = 99;
          Integer integerValue = new Integer(num);
          // converting int to double
	      // using doubleValue() method
          //of Interger  
          Double doubleValue = integerValue.doubleValue();
       
          //print the double value
          System.out.println("doubleValue :"+doubleValue);
	}
}

Output:

doubleValue :99.0

4. Convert Int to Double using assignment operator – Widening Casting

In java int is a smaller datatype as compare double datatype. So, when you assign an int value to double variable, then automatically the conversion of int to double happens in Java. This is also called widening casting.

So, to converting int to double java using widening casting, you just need assign a value of int to double using assignment operator. Let’s see casting int to double in java using assignment operator . In this example we create int variable then cast int to double using just assign.

public class IntToDoubleJava3 {
	public static void main(String[] args) {
	    //  initialize  int variable 
	    int num = 99;
	        
	    //conversion of int to double 
	    double doubleValue =  num;  
	    
        //print the double value
	    System.out.println("doubleValue : "+doubleValue);
	}

}

OutPut:

doubleValue : 99.0

In this tutorial, we learned different method of java convert int to double . Cast int to double is ask frequently question in many interview . You should know how to convert int to double in java . You can see more java program for interview preparation .

Leave a Reply

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

38 + = 44