The Float class wraps a value of primitive type float in an object. An object of type Float contains a single field whose type is a float.
In addition, this class provides several methods for converting a float to a String and a String to a float, as well as other constants and methods useful when dealing with a float.
Float class Constructors
- Float(double value) - Constructs a newly allocated Float object that represents the argument converted to type float.
- Float(float value) - Constructs a newly allocated Float object that represents the primitive float argument.
- Float(String s) - Constructs a newly allocated Float object that represents the floating-point value of type float represented by the string.
Example:
float b = 55.05F;
String str = "45";
double d = 10.10;
// Construct three Float objects
Float x = new Float(b);
Float y = new Float(str);
Float z = new Float(d);
Float class Methods
The class diagram shows a list of APIs/Methods that Float class provides.
In this post, we will learn a few important methods of Float wrapper class.
Example: This program demonstrates usage of Float wrapper class methods.
package com.javaguides.corejava.wrapperclasses;
/**
* This class to demonstrate Float wrapper class methods
* @author javaguides.net
*
*/
public class FloatClassExample {
public static void main(String[] args) {
float b = 55.05F;
String str = "45";
double d = 10.10;
// Construct two Float objects
Float x = new Float(b);
Float y = new Float(str);
// toString()
System.out.println("toString(b) = " + Float.toString(b));
// valueOf()
// return Float object
Float z = Float.valueOf(b);
System.out.println("valueOf(b) = " + z);
z = Float.valueOf(str);
System.out.println("ValueOf(bb) = " + z);
// parseFloat()
// return primitive float value
float zz = Float.parseFloat(str);
System.out.println("parseFloat(bb) = " + zz);
System.out.println("bytevalue(x) = " + x.byteValue());
System.out.println("shortvalue(x) = " + x.shortValue());
System.out.println("intvalue(x) = " + x.intValue());
System.out.println("longvalue(x) = " + x.longValue());
System.out.println("doublevalue(x) = " + x.doubleValue());
System.out.println("floatvalue(x) = " + x.floatValue());
int hash = x.hashCode();
System.out.println("hashcode(x) = " + hash);
boolean eq = x.equals(y);
System.out.println("x.equals(y) = " + eq);
int e = Float.compare(x, y);
System.out.println("compare(x,y) = " + e);
int f = x.compareTo(y);
System.out.println("x.compareTo(y) = " + f);
Float d = Float.valueOf("1010.54789654123654");
System.out.println("isNaN(d) = " + d.isNaN());
System.out.println("Float.isNaN(45.12452) = " + Float.isNaN(45.12452F));
// Float.POSITIVE_INFINITY stores
// the positive infinite value
d = Float.valueOf(Float.POSITIVE_INFINITY + 1);
System.out.println("Float.isInfinite(d) = " + Float.isInfinite(d.floatValue()));
float dd = 10245.21452F;
System.out.println("Float.toString(dd) = " + Float.toHexString(dd));
int float_to_int = Float.floatToIntBits(dd);
System.out.println("Float.floatToLongBits(dd) = " + float_to_int);
float int_to_float = Float.intBitsToFloat(float_to_int);
System.out.println("Float.intBitsToFloat(float_to_long) = " + int_to_float);
}
}
Output:
toString(b) = 55.05
valueOf(b) = 55.05
ValueOf(bb) = 45.0
parseFloat(bb) = 45.0
bytevalue(x) = 55
shortvalue(x) = 55
intvalue(x) = 55
longvalue(x) = 55
doublevalue(x) = 55.04999923706055
floatvalue(x) = 55.05
hashcode(x) = 1113338675
x.equals(y) = false
compare(x,y) = 1
x.compareTo(y) = 1
isNaN(d) = false
Float.isNaN(45.12452) = false
Float.isInfinite(d) = true
Float.toString(dd) = 0x1.4029b8p13
Float.floatToLongBits(dd) = 1176507612
Float.intBitsToFloat(float_to_long) = 10245.215
Comments
Post a Comment
Leave Comment