Can we convert int to double in java

Java – Convert int to double example

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.

Читайте также:  Java number to bigdecimal

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.

Java Convert int to double

Output:

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.

Convert int to double in Java using Double wrapper class

Output:

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

Источник

Can we convert int to double in java

Java int to Double Example

We can convert int value to Double object by instantiating Double class or calling Double.valueOf() method.

Let’s see the simple code to convert int to Double in java.

Youtube

For Videos Join Our Youtube Channel: Join Now

Feedback

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials

Splunk tutorial

SPSS tutorial

Swagger tutorial

T-SQL tutorial

Tumblr tutorial

React tutorial

Regex tutorial

Reinforcement learning tutorial

R Programming tutorial

RxJS tutorial

React Native tutorial

Python Design Patterns

Python Pillow tutorial

Python Turtle tutorial

Keras tutorial

Preparation

Aptitude

Logical Reasoning

Verbal Ability

Company Interview Questions

Artificial Intelligence

AWS Tutorial

Selenium tutorial

Cloud Computing

Hadoop tutorial

ReactJS Tutorial

Data Science Tutorial

Angular 7 Tutorial

Blockchain Tutorial

Git Tutorial

Machine Learning Tutorial

DevOps Tutorial

B.Tech / MCA

DBMS tutorial

Data Structures tutorial

DAA tutorial

Operating System

Computer Network tutorial

Compiler Design tutorial

Computer Organization and Architecture

Discrete Mathematics Tutorial

Ethical Hacking

Computer Graphics Tutorial

Software Engineering

html tutorial

Cyber Security tutorial

Automata Tutorial

C Language tutorial

C++ tutorial

Java tutorial

.Net Framework tutorial

Python tutorial

List of Programs

Control Systems tutorial

Data Mining Tutorial

Data Warehouse Tutorial

Javatpoint Services

JavaTpoint offers too many high quality services. Mail us on h[email protected], to get more information about given services.

  • Website Designing
  • Website Development
  • Java Development
  • PHP Development
  • WordPress
  • Graphic Designing
  • Logo
  • Digital Marketing
  • On Page and Off Page SEO
  • PPC
  • Content Development
  • Corporate Training
  • Classroom and Online Training
  • Data Entry

Training For College Campus

JavaTpoint offers college campus training on Core Java, Advance Java, .Net, Android, Hadoop, PHP, Web Technology and Python. Please mail your requirement at [email protected].
Duration: 1 week to 2 week

Like/Subscribe us for latest updates or newsletter RSS Feed Subscribe to Get Email Alerts Facebook Page Twitter Page YouTube Blog Page

Источник

How to Convert int to double in Java

In Java, the most popular primitive data types are “double” and “int“. The double data type is more extensive than the int type because it stores 64-bit floating-point numbers and takes more memory space, whereas the integer type stores 32-bit integers. Java implicitly converts int values to double. However, you may need to perform this int to double conversion explicitly.

This blog will describe the method for converting int to double in Java.

How to Convert int to double in Java?

For converting int to double, you can use the:

We will now check out each of the mentioned methods one by one.

Method 1: Convert int to double Using Assignment Operator

In Java programming language, the lower data type can be easily converted to the higher data type using the Assignment operator “=”. This is called an implicit conversion.

Here, the assignment operator “=” will convert “a” int type variable to “b”, which is a double type variable.

Example
In this example, firstly, we will create an int variable named “a” with the following value:

Then, we will convert it to double using the “=” assignment operator and store the resultant value in “b”:

Lastly, execute the “System.out.println()” method to display the converted value on the console:

The output shows that the integer is successfully converted into a double value:

Method 2: Convert int to double Using Typecasting

Typecasting is used when we want to convert one datatype to another. More specifically, it can also be utilized for int to double conversion.

Here, we will convert “a” int type variable to “b”, which is a double type variable. The (double) indicates the required typecasted data type.

Example
In this example, we will use the same integer type “a” variable and convert its value to “double” using Typecasting. Here, the assignment operator is also used; however, the specified integer is typecasted in the double and then stored in the double type variable “b”:

Then, print out the converted value using the “System.out.println()” method:

Want to utilize any built-in Java method for the specified purpose? Head toward the next section!

Method 3: Convert int to double Using valueOf() Method

The “Double” Java wrapper class offers a “valueOf()” method that can be used to convert int to double. It is a static type method, which means we do not need to create an object and call the method by using the class name, as it can be accessed without this additional step.

Here, we will convert “a” int type variable to “b” by passing it as an argument to the “valueOf()” method.

Example
Here, we will convert the value of the already created “a” variable using the valueOf() method. The method will take “a” as an argument and returns the converted double value:

Finally, print out the converted value using the “System.out.println()” method:

We compiled all the essential instructions related to converting int to double in Java.

Conclusion

To convert int to double in Java, there are three methods: using the Assignment operator, using Typecasting, and the valueOf() method of the Double Java wrapper class. All these methods approximately work the same; however, you may choose any one depending on your preferences. In this post, we described the methods to convert an int to double in Java.

About the author

Farah Batool

I completed my master’s degree in computer science. I am an academic researcher and love to learn and write about new technologies. I am passionate about writing and sharing my experience with the world.

Источник

Convert Int to Double in Java

Convert Int to Double in Java

  1. int in Java
  2. double in Java
  3. Implicitly Convert Int to Double Without Utilizing Typecasting in Java
  4. Implicitly Convert Int to Double by Utilizing Typecasting in Java
  5. Convert Int to Double Using the Double Wrapper Class in Java
  6. Conversion Reliant on Java’s Automatic Type Recognition

The difference between int and double is that double is used to store 64-bit double-precision floating-point value, and int is used in the storage of a 32-bit integer.

int in Java

int is a primitive data type that is allocated 4 bytes of memory by most systems. It is used to store integer values, and its operations are faster than those of doubles. See the code below.

public class intOperation  public static void main(String[] args)   int a = 20;  int b = 3;   int c= a + b;  System.out.println(c);  > > 

In the above code, a and b are variables of type int that store 20 and 3, respectively. The sum of the two int variables results in another int variable 23.

double in Java

double is a primitive data type that is allocated 8 bytes of memory by most systems. It is used to store floating-point values (values that are either a fraction or end with a fraction). The arithmetic computation of doubles by the system is slower than that of int. See the code example below.

public class doubleOperation  public static void main(String[] args)   double a = 20.5;  double b = 5.0;   double c= a + b;  System.out.println(c);  > > 

In the above code, a and b are variables of type double that store 20.5 and 5.0, respectively. The sum of the two double variables results in another double variable 25.5.

Implicitly Convert Int to Double Without Utilizing Typecasting in Java

The implicit conversion of int to double relies on the fact that double data types have a bigger memory size and a wider range. See the code below:

public class intToDouble  public static void main(String args[])  //the int value  int a = 55;   //conversion of int to double  double b = a;   System.out.println(b);  > > 

Implicitly Convert Int to Double by Utilizing Typecasting in Java

Like we have done in the previous method, we use an assignment operator, but we cast it to type double. See the code below:

public class intToDouble  public static void main(String args[])  //the int value  int a = 55;   //conversion of int to double  double b = (double) a;   System.out.println(b);  > > 

Convert Int to Double Using the Double Wrapper Class in Java

In this method, we use the double wrapper class’ valueOf() method. This method is direct and easy to implement repeatedly. It takes the int as a parameter and returns a double value. See the code below.

public class intToDouble  public static void main(String args[])  //the int value  int a = 55;   //conversion of int to double  double b = Double.valueOf(a);   System.out.println(b);  > > 

Conversion Reliant on Java’s Automatic Type Recognition

This is a direct method where we multiply the int variable with a double value to get the result as a double. See the code below.

public class intToDouble  public static void main(String args[])  //the int value  int a = 55;   //conversion of int to double  double b = 1.0 * a;   System.out.println(b);  > > 

Related Article — Java Int

Related Article — Java Double

Источник

How to Convert Int to Double in Java

Scientech Easy

In this tutorial, we will learn how to convert int numeric value to double value, int value to Double object and vice versa in Java easily.

To convert int to double numeric value in Java, we use an assignment operator.

We do not need to do extra to convert int to double. This is because the lower type can be converted to a higher type implicitly.

We also known this conversion as implicit type casting or type promotion.

Let’s create a simple Java program to convert int to double easily.

Program code 1:

// Java Program to demonstrate the conversion of int into double implicitly. package javaConversion; public class IntToDoubleConversion < public static void main(String[] args) < int num = 900; double d = num; System.out.println("Double numeric value after conversion: " +d); >>
Output: Double numeric value after conversion: 900.0

Converting Int to Double object in Java using Double.valueOf() method

We can convert int numeric value to Double object by creating an object of Double wrapper class and then call its valueOf() method.

Let’s make a simple Java program for converting int to Double object using valueOf() method of Double class.

Program code 2:

// Java Program to demonstrate the conversion of int into double object. package javaConversion; public class IntToDoubleConversion < public static void main(String[] args) < int num = 900; Double d1 = new Double(num); // first way Double d2 = Double.valueOf(num); // second way System.out.println(d1); System.out.println(d2); >>

In the above program, the valueOf() method of Double wrapper class returns a Double instance representing the specified double value.

Converting Double to Int in Java using Typecasting

To convert double to int numeric value in Java, we use typecasting. To convert the higher data type to lower, we perform typecasting in the program through a typecast operator (datatype).

Let’s make a Java program for converting double primitive type into int value using typecasting.

Program code 3:

// Java Program to demonstrate the conversion of double into int value. package javaConversion; public class DoubleToIntConversion < public static void main(String[] args) < double d = 99.99; int i = (int)d; // typecasting. System.out.println(i); >>

Double object to int Conversion

For converting Double object to int value in Java, we use intValue() method of Double class. Let’s take a simple program for converting Double object to int in Java.

Program code 4:

// Java Program to demonstrate the conversion of Double object into int value using intValue() method. package javaConversion; public class DoubleToIntConversion < public static void main(String[] args) < Double d = new Double(999.999); int i = d.intValue(); System.out.println(i); >>

In this example program, we have used intValue() method of Double class for converting Double object to int value. The intValue() method returns the value of this Double as an int after a narrowing primitive conversion.

In this tutorial, you learned how to convert a primitive type int value to double and vice versa in Java easily. Hope that you will have understood and practiced all programs based on the conversion of int to double in Java.
Thanks for reading.
Next ⇒ Convert Char to Int in Java ⇐ Prev Next ⇒

Источник

Оцените статью