Sunday, November 17, 2024
Google search engine
HomeLanguagesJavaJava Program to Convert Int to Double

Java Program to Convert Int to Double

Java data types can be categorized as primitive and non-primitive. Primitive data types contain a single value, whereas non-primitive data types contain an address of the variable value. Java supports 7 primitive data types – boolean, byte, char, short, int, long, float, double. These data types require a different amount of space while stored in memory.

Data Type

Bits Acquired in Memory

boolean

1

byte

8

char

16

short

16

int

32

long

64

float

32

double

64

Int stores 32-bit signed two’s complement integer and require 32 bits (4 bytes) to be stored in memory while double stores double-precision 64-bit floating-point numbers and require 64 bits (8 bytes) for storage. Many times int values need to be converted to double so, to perform such conversion following three methods can be used:

  • Conversion using assignment operator or Implicit Conversion
  • Conversion using Double class constructor
  • Conversion using valueOf() method

1. Conversion using an assignment operator

Java performs implicit conversion or widening conversion when both data types involved in conversion are compatible, or the value of a smaller data type is assigned to a bigger data type. Double is bigger than int as it requires more storage space; hence, int values are implicitly converted to double by Java.

Syntax:

double d = i

Here,

  • d = variable to store double value after conversion to double data type
  • i = integer value to be converted to double data type

Java




// Java program to convert int to 
// double using assignment operator
  
class GFG {
    public static void main(String[] args)
    {
        int i = 100;
        // Implicit conversion from int to double data type
        double d = i;
  
        System.out.println("Integer value " + i);
        System.out.println("Double value " + d);
    }
}


Output

Integer value 100
Double value 100.0

2. Conversion using Double wrapper class constructor

Double class in Java is a wrapper class used to create objects that can hold single, double type values and contain several methods to deal with double matters. Int value can be passed to the constructor of the Double class to create an object of double type initialized with provided integer value.

Syntax:

Double d = new Double(i)

Here,

  • d = variable to store double value after conversion to double data type
  • i = integer value to be converted to double data type

Java




// Java program to convert int to double 
// using Double wrapper class constructor
  
class GFG {
    public static void main(String[] args)
    {
        int i = 100;
        // Conversion of int to double data 
          //type using Double wrapper class
        Double d = new Double(i);
  
        System.out.println("Integer value " + i);
        System.out.println("Double value " + d);
    }
}


Output

Integer value 100
Double value 100.0

3. Conversion using valueOf() method of Double wrapper class

Double wrapper class valueOf() method converts int value to double type. It returns the Double-object initialized with the provided integer value. 

Syntax: 

Double d = Double.valueOf(i)

Here,

  • d = variable to store double value after conversion to double data type
  • i = integer value to be converted to double data type

Java




// Java program to convert int 
// to double using valueOf() method
  
class GFG {
    public static void main(String[] args)
    {
        int i = 100;
        // Conversion of int to double data type using valueOf() method
        Double d = Double.valueOf(i);
  
        System.out.println("Integer value " + i);
        System.out.println("Double value " + d);
    }
}


Output

Integer value 100
Double value 100.0
Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments