×

How to Convert int to double in Java

How to Convert int to double in Java

When two variables of different types are involved in the single expression, Java compiler uses built-in library function to convert the variable to common data type before evaluating the expression. Java compiler follows certain rules to decide which variable’s data type is to be converted. It converts the lower data type to that of a higher data type implicitly.

The double data type has a longer range than an int data type. In Java, JVM automatically converts int value to double when we assign an integer value to double data type.

In the above figure, the left-side value can assign to any right-side value, and it is done implicitly. It is known as implicit type conversion or type promotion or widening conversion. There are two methods to convert int to double.

  • Using Assignment Operator (without typecasting)
  • Using Double.valueOf() method

Using Assignment Operator

Assignment of int to double is done implicitly. You can convert int to double by using assignment operator.

Example

In the following example, we have taken a variable s of type int and initialize the value 20 to it. We have assigned the variable s to the variable d of type double. The println statement prints the double value of s.

public class intTodoubleExample
{
public static void main(String args[])
{
int s=20;
double d=s;
System.out.println(“double value of s is: ”+d);
}
}

Output

double value of s is: 20.0

Using Double.valueOf() method

You can also convert int to double using the valueOf() method of Double wrapper class. This method accepts the int type value as parameter and returns the same value converted in double type.

Example

In this example, we have taken two variables i and r of type int and initialized 50 and 29 to it, respectively. Double is a wrapper class, and d is an object of Double class. It parses the integer value i as an argument and returns the converted double value. The valueOf() is the method of Double class. It parses an integer value r as an argument and returns the converted double value of the variable r. The first println statement prints the double value of the variable i. and the second println statement prints the double value of the variable r.

public class intTodoubleExample1
{
public static void main(String args[])
{
int i=50;
int r=29;
Double d= new Double(i); //Using object of Double class
Double dbl= Double.valueOf(r);             //Using valueOf() method
System.out.println("double value of i is: "+d);
System.out.println("double value of r is: "+dbl);
}
}

Output

double value of i is: 50.0
double value of r is: 29.0

Related Topics

Process and Thread in Java

Process It is a program that is running on your computer. The process is a heavy-weight, which takes memory separately to other processes. The process may be a small background task like spell-checker; it...

11 minutes read.

Features of Java

The objective of the Java programming language is to provide portability, security, and simplicity. In spite of this, Java has a number of features that makes it a popular language...

5 minutes read.

Java Custom Exception

Java Custom Exception Java language facilitates us to create our own exceptions. The class intended to throw the Custom Exception has to be derived from the Java Exceptionor RuntimeExceptionclass. The Java...

4 minutes read.

How to achieve Multiple Inheritance in Java

Multiple Inheritance is the type of inheritance where one class inherits the properties of more than one super class. For example, class Child inherits (extends) the classes Father and Mother....

4 minutes read.

Java Integer toOctalString() method

The toOctalString() method of Java Integer class returns a string representing the specified int argument as an unsigned integer in base 8. Syntax public static String toOctalString (int  i) Parameters The parameter ‘i’ represents...

1 minute read.

Swing Program in Java

Java Swing is part of Java Foundation Classes (JFC). The swing toolkit is used to generate Graphical User Interface (GUI) for programs written in Java. The Java swing API is...

4 minutes read.

Sorting a String in Java

Sorting a String in Java Sorting is a process of arranging available data objects in a meaningful format that is ascending or descending order. Sorting a string or array of String...

4 minutes read.

How to Set Java_home in Linux

To set the Java_home in Linux, we must follow several steps to make us understand it easily.  Java follows the principle called WORA (write once run anywhere). We know that java...

3 minutes read.

For Loop Program in Java

For Loop Program in Java The for-loop program in Java is written when we want a particular set of statements to be executed repeatedly until the given criteria/ condition is met....

8 minutes read.

Console Errors in Java

An unlawful motion taken through the person that reasons this system to act abnormally is amistake until this system is compiled or run; maximum programming mistakes pass unnoticed.The software is...

3 minutes read.

Shift right zero Fill Operator in Java and Operator Shifting

Left shift operator ( << ) : The left shift operator is an operator which performs its action at the bits level of a binary Operator. That means when you perform...

4 minutes read.

Java vs Node.js

Java: Java is an object oriented programming language. It is also known as multi threaded language. It was designed by James gosling in the year 1995. We can also say that...

4 minutes read.

Java Enum

Enumerations are used in programming languages to represent collections of named constants. For instance, the four suits in a deck of playing cards might represent four iterators named Club, Diamond,...

3 minutes read.

Java Localization

Internationalization is the process of creating a software application that can be translated into different languages and regions without modifying the application. Creating a locale-specific application raises the cost of...

3 minutes read.

Minimum Window Subsequence in Java

In this article, you will be very well acknowledged about the minimum window subsequence, what is the approach and how it is implemented. The example program is also executed and...

4 minutes read.

Java throw

Java throw Sometimes it is required in the code to throw an exception deliberately. In order to achieve the same, the Java throw keyword should be used. One can throw either...

3 minutes read.

Practical Number in Java

In this tutorial, we will understand what is meant by practical numbers. We will understand it throughthe aid of examples and implementation in a java programming language. The practical numbers...

5 minutes read.

How to Assign Static Value to Date in Java?

In this tutorial, we will learn certain ways to assign a static value to a date in java. We will see the limitation of each method that will lead to...

3 minutes read.

Singleton class in Java

What is Singleton class in Java? Singleton means it is one. That means we can create only one instance or object of a class. For example, let us have a class...

3 minutes read.

Java Int Keyword

Among the primitive data types is the Java int keyword. To declare variables, use this. It can also be used with methods that return values of the integer type. It...

3 minutes read.