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