beginnersbook.com

  • Home
  • All Tutorials
    • Learn Servlet
    • Learn JSP
    • Learn JSTL
    • Learn C
    • Learn C++
    • Learn MongoDB
    • Learn XML
    • Learn Python
    • Learn Perl
    • Learn Kotlin
    • Learn jQuery
  • Core Java
  • OOPs
  • Collections
  • Java I/O
  • JSON
  • DBMS

Java – Convert int to double example

By Chaitanya Singh | Filed Under: Java Conversion

In this java tutorial, we will see how to convert int to double in Java. Since double has longer range than int data type, java automatically converts int value to double when the int value is assigned to double.

1. Java implicit conversion from int to double without typecasting.
2. Java – Convert int to double using Double wrapper class.

1. Java implicit conversion from int to double without typecasting

Since double data type has wider range and greater memory size than int, the conversion from int to double is implicit. As you can see we have not done the typecasting like we did in double to int conversion in Java.

public class JavaExample{  
   public static void main(String args[]){ 
	//int value
	int inum = 101;
		
	//int to double conversion without typecasting
	double dnum = inum;  
		
	//displaying double value
	System.out.println(dnum);  
   }
}

Output:
Java Convert int to double

2. Java – Convert int to double using Double wrapper class

In this example we are doing the int to double conversion using the valueOf() method of Double wrapper class. This method accepts the other type value as parameter and returns the same value converted in double type.

public class JavaExample{  
   public static void main(String args[]){ 
	//int value
	int inum = 55;
		
	/* valueOf() method of Double class converts the passed
	 * value to the double value.
	 */
	Double dnum = Double.valueOf(inum);
		
	//displaying output
	System.out.println(dnum);
		
   }
}

Output:
Convert int to double in Java using Double wrapper class

❮ PreviousNext ❯

Leave a Reply Cancel reply

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

Java Conversion

  • Java String to int
  • Java int to String
  • Java String to long
  • Java long to String
  • Java String to double
  • Java double to String
  • Java double to int
  • Java int to double
  • Java int to long
  • Java long to int
  • Java char to String
  • Java char to int
  • Java int to char
  • Java float to String
  • Java boolean to String
  • Java String to boolean
  • Java binary to decimal
  • Java decimal to binary
  • Java binary to Octal
  • Java decimal to hexadecimal
  • Java Hex to decimal
  • Java decimal to Octal
  • Java Octal to decimal
  • Java ASCII to String
  • Java Writer to String
  • Java StackTrace to String
  • Java String to ArrayList
  • Java StringBuffer to String
  • Java InputStream to String

Recently Added..

  • JSON Tutorial
  • Java Regular Expressions Tutorial
  • Java Enum Tutorial
  • Java Annotations Tutorial

Copyright © 2012 – 2022 BeginnersBook . Privacy Policy . Sitemap