Java Program to convert Double to numeric primitive data types


Let’s say we have Double object here.

Double obj = new Double("39.45");

Now, if we want to convert this Double to short primitive data type. For that, use the in-built shortValue() method.

// converting to short primitive types
short shortObj = obj.shortValue();
System.out.println(shortObj);

In the same way convert Double to another numeric primitive data type int. For that, use the in-built intValue() method.

// converting to int primitive types
int intObj = obj.intValue();
System.out.println(intObj);

The following is an example wherein we convert Double to numeric primitive types short, int, float, etc.

Example

 Live Demo

public class Demo {
   public static void main(String args[]) {
      Double obj = new Double("39.45");
      int intVal = obj.intValue();
      System.out.println(intVal);
      byte byteVal = obj.byteValue();
      System.out.println(byteVal);
      short shortVal = obj.shortValue();
      System.out.println(shortVal);
      float floatVal = obj.floatValue();
      System.out.println(floatVal);
      double doubleVal = obj.doubleValue();
      System.out.println(doubleVal);
   }
}

Output

39
39
39
39.45
39.45

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 26-Jun-2020

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements