This example shows how to convert Long object to numeric primitive types like byte, short, int, long, float, and double.
How to convert Long object to numeric primitive types in Java?
Use the xxxValue method of the Long wrapper class to convert a Long object to primitive numeric types where the xxx is the primitive type you want to convert to as given below.
1) Convert Long to byte
Use the byteValue
method of the Long wrapper class to convert from Long to byte primitive value.
1 |
public byte byteValue() |
This method returns the value of the Long object as a byte primitive.
2) Convert Long to short
Use the shortValue
method of the Long wrapper class to convert from Long to short primitive value.
1 |
public short shortValue() |
This method returns the value of the Long object as a short primitive.
3) Convert Long to int
Use the intValue
method of the Long wrapper class to convert from Long to int primitive value.
1 |
public int intValue() |
This method returns the value of the Long object as an int primitive.
4) Convert Long to long
Use the longValue
method of the Long wrapper class to convert from Long to long primitive value.
1 |
public long longValue() |
This method returns the value of the Long object as a long primitive.
5) Convert Long to float
Use the floatValue
method of the Long wrapper class to convert from Long to float primitive value.
1 |
public float floatValue() |
This method returns the value of the Long object as a float primitive.
6) Convert Long to double
Use the doubleValue
method of the Long wrapper class to convert from Long to double primitive value.
1 |
public double doubleValue() |
This method returns the value of the Long 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 ConvertLongToPrimitiveExample { public static void main(String[] args) { Long l = new Long("81"); System.out.println("Long to byte: " + l.byteValue() ); System.out.println("Long to short: " + l.shortValue() ); System.out.println("Long to int: " + l.intValue() ); System.out.println("Long to long: " + l.longValue() ); System.out.println("Long to float: " + l.floatValue() ); System.out.println("Long to double: " + l.doubleValue() ); } } |
Output
1 2 3 4 5 6 |
Long to byte: 81 Long to short: 81 Long to int: 81 Long to long: 81 Long to float: 81.0 Long to double: 81.0 |
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.