×

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

Advanced Java skills

These were some of the contemporary talents that a Java developer should master in efforts to progress in the era of science in 2022. A database such as MySQL, a...

4 minutes read.

Unicode System in Java

In this tutorial, we will understand the meaning and emergence of the Unicode system in java. The Unicode system is one of the important and useful features in the world...

6 minutes read.

Java Math subtractExact() Method

The subtractExact() method of Java Math class returns mathematical difference of the specified two arguments, throwing an exception if the result overflows int or long. Syntax: public static int subtractExact (int x,...

1 minute read.

Java Else Keyword

The else statement specifies a section of Java code that will run if an if statement's condition is false.The following conditional statements can be used in Java:To provide a block...

3 minutes read.

How to enable java in chrome

The Java module is huge for the Java Runtime Environment (JRE). It permits a program to work with the Java stage to run Java applets. Essentially, each of the undertakings connect...

3 minutes read.

Java Finally Keyword

The final block in Java is used to run essential code, such as connection closure, among other things. Whether an exception is resolved or not, the Java finally block has...

3 minutes read.

What is anagram in Java?

In this section will explain what an anagram is in Java and demonstrate how to determine whether or not a text is an anagram. In Java interviews, the anagram Java...

4 minutes read.

How to Convert Date to String in Java

How to Convert Date to String in Java We need to convert Date to String in Java may for displaying purpose. We can convert Date to String in Java using the format() method of java.text.DateFormat class. There...

2 minutes read.

How to Calculate Time Difference Between Two Dates in Java?

Date is being used extensively in Java to calculate date differences. While constructing an application, the date of joining an organisation, admission date, appointment date, and others might be included....

4 minutes read.

How to Set Environment Variables for Java

Introduction Java is an object-oriented programming language that is based on classes and can be employed mostly to develop web and desktop applications. No matter the computer architecture, Java applications are...

7 minutes read.

Group by in Java 8

By using this groupingBy() method, developers can directly able to perform the "GROUP BY" operation. The thing is, now, the Java 8 programming language allows the programmers to do this...

3 minutes read.

Java Protected Keyword

An access modifier is a keyword that Java protects. It can be used to constructors, methods, inner classes, and variables. Variables, methods, and constructors that have been marked protected in...

3 minutes read.

Java 8 Features

Java 8 Features: After the great success of Java 7, many developers were waiting for the next version of one of the best languages in the tech world. Moreover, on...

6 minutes read.

Java LinkedList vs ArrayList

LinkedList In LinkedList, each element is a distinct entity containing an information portion and an address component, and the elements are not kept in consecutive locations. Pointers & addresses are used...

3 minutes read.

Getting the Day from the Date in Java?

To extract the day's name from the date, we will write Java application. When dealing the date and time in Java, the following classes are used. Class for Calendars: This class is...

6 minutes read.

Convert Char array to string in java

A collection of characters is referred to as a string. A character array differs from a string in that the string is canceled by the special character "\0." A string...

4 minutes read.

Java String startsWith() method

Java String startsWith() method checks whether current String starts with given prefix or not . Syntax: public boolean startsWith(String prefix) public boolean startsWith(String prefix, int offset) Parameters: prefix : It is sequence of character Returns: It returns...

2 minutes read.

New Features of Java 14

 Features like Switch expressions and text blocks which are previewed in Java 13 version are standardized in Java 14. Features in Java 14 Switch ExpressionText BlocksRecordsNull Pointer ExceptionsInstance ofPackaging ToolGarbage collectors 1.Switch Expressions Switch...

3 minutes read.

Java Console

If there is a character-based console device connected to the active Java virtual machine, it can be accessed using methods provided by the Java.io.Console class. JDK 6 adds the Console...

3 minutes read.

Median Of Stream Of Running Integers in Java

An integer array is given to us. Compute the median of the elements traversed so far in the input array. For the sake of simplicity, assume that there are no...

10 minutes read.