This example shows how to convert Double object to a numeric primitive types byte, short, int, long, float, double.
How to convert a Double object to numeric primitive types in Java?
We can use the xxxValue method of the Double wrapper class to convert a Double object to primitive numeric types, where the xxx is the primitive type we want to convert to as given below.
1) Convert Double to byte
Use the byteValue
method of Double wrapper class to convert from Double to byte primitive value.
1 |
public byte byteValue() |
This method returns the value of the Double object as a byte primitive.
2) Convert Double to short
Use the shortValue
method of Double wrapper class to convert from Double to a short primitive value.
1 |
public short shortValue() |
This method returns the value of the Double object as a short primitive.
3) Convert Double to int
Use the intValue
method of Double wrapper class to convert from Double to int primitive value.
1 |
public int intValue() |
This method returns the value of the Double object as an int primitive.
4) Convert Double to long
Use the longValue
method of Double wrapper class to convert from Double to long primitive value.
1 |
public long longValue() |
This method returns the value of the Double object as a long primitive.
5) Convert Double to float
Use the floatValue
method of Double wrapper class to convert from Double to float primitive value.
1 |
public float floatValue() |
This method returns the value of the Double object as a float primitive.
6) Convert Double to double
Use the doubleValue
method of Double wrapper class to convert from Double to double primitive value.
1 |
public double doubleValue() |
This method returns the value of the Double object as a double primitive.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
package com.javacodeexamples.basic.wrapperclasses; public class ConvertDoubleToPrimitiveExample { public static void main(String[] args) { Double d = new Double("6.5512486728"); System.out.println("Double to byte: " + d.byteValue() ); System.out.println("Double to short: " + d.shortValue() ); System.out.println("Double to int: " + d.intValue() ); System.out.println("Double to long: " + d.longValue() ); System.out.println("Double to float: " + d.floatValue() ); System.out.println("Double to double: " + d.doubleValue() ); } } |
Output
1 2 3 4 5 6 |
Double to byte: 6 Double to short: 6 Double to int: 6 Double to long: 6 Double to float: 6.5512486 Double to double: 6.5512486728 |
This example is a part of the Java Basic Examples and Java Type conversion Tutorial.
Please let me know your views in the comments section below.