- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to Convert Int to Double in Java?
In Java, smaller datatype can be converted to bigger datatype. Here, we will see how to convert int datatype to double datatype.
The int data type is a 32-bit signed two's complement integer. Its value-range lies between - 2,147,483,648 (-2^31) to 2,147,483,647 (2^31 -1).
The double data type is a double-precision 64-bit IEEE 754 floating point. Its range of values is beyond the scope of this discussion, but is specified in the Floating-Point Types, Formats, and Values section of the Java Language Specification.
Let’s deep dive into this article, to know how it can be done by using Java programming language.
To show you some instances
Instance-1
Suppose the given int datatype is ‘1021’.
Then it will be converted to double datatype and result will be −
Double value is: 1021.0
Instance-2
Suppose the given int datatype is ‘3265’.
Then it will be converted to double datatype and result will be −
Double value is: 3265.0
Instance-3
Suppose the given int datatype is ‘102’.
Then it will be converted to double datatype and result will be −
Double value is: 102.0
Algorithm
Step-1 − Declare the int datatype.
Step-2 − Convert it into double datatype.
Convert Implicitly
Using Double wrapper class
valueOf() method
Step-3 − Print the result.
Syntax
valueOf() − In java, valueOf() method converts different types of values like float, byte, int etc into string.
Multiple Approaches
We have provided the solution in different approaches.
By Using Assignment Operator
By Using Double Class Constructor
By Using valueOf() method
Let’s see the program along with its output one by one.
Approach-1: By Using Assignment Operator
In this approach, int will be converted to double by simply assigning the int datatype to double datatype. This is done by compiler implicitly and is known as implicit type casting.
Example
public class Main { //main method public static void main(String args[] ){ //Declaring int datatype int i = 1021; // Implicit conversion from int to double data type double d = i; //printing int datatype System.out.println("Integer value is: " + i); //printing double datatype System.out.println("Double value is: " + d); } }
Output
Integer value is: 1021 Double value is: 1021.0
Approach-2: By Using Double Class Constructor
In this approach, int value can be passed to the constructor of the Double class to create an object of double type. Double class is a wrapper class used to create objects that can hold single, double type values and contain several methods to deal with double matters.
Example
public class Main { //main method public static void main(String args[]){ //Declaring int variable int i = 3265; //int to double datatype conversion using Double wrapper class Double d = Double.valueOf(i); //printing int datatype System.out.println("Integer value is: " + i); //printing double datatype System.out.println("Double value is: " + d); } }
Output
Integer value is: 3265 Double value is: 3265.0
Approach-3: By Using valueOf() Method
In this approach, valueOf() method is used. valueOf() method belongs to Long Wrapper class. It accepts an integer as an argument and returns a double value after the conversion.
Example
public class Main { //main method public static void main(String[] args){ //Declaring int datatype int i = 100; //int to double data type conversion using valueOf() method Double d = Double.valueOf(i); //printing int datatype System.out.println("Integer value is: " + i); //printing double datatype System.out.println("Double value is: " + d); } }
Output
Integer value is: 100 Double value is: 100.0
In this article, we explored different approaches to convert int to Double by using Java programming language.