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