Java float type
In this chapter you will learn:
- What is Java float type
- How to create float literals
- How to Create a Float object
- How to tell if a float is infinite or finite
- How to convert float value to int bits
float type
float
type represents single-precision numbers.
float is 32-bit width and its range is from 1.4e-045
to 3.4e+038
approximately.
float
type variables are useful when you need a fractional component.
Here are some example float variable declarations:
float high, low;
float literals
Floating-point literals in Java default to double precision. To specify a float literal, you must append an F or f to the constant.
public class Main {
public static void main(String args[]) {
float d = 3.14159F;
System.out.print(d);//3.14159
} /*from j av a2 s . com*/
}
Java's floating-point calculations are capable of returning +infinity, -infinity, +0.0, -0.0, and NaN
public class Main {
public static void main(String[] args) {
Float f1 = new Float(Float.NaN);
System.out.println(f1.floatValue());
// ja va2s. c om
Float f2 = new Float(Float.NaN);
System.out.println(f2.floatValue());
System.out.println(f1.equals(f2));
System.out.println(Float.NaN == Float.NaN);
System.out.println();
}
}
Float class wraps a value of type float in an object.
Create a Float object
We can create Float
object in three ways:
Float(double value)
creates a Float object from double-type value and convert it to type float.Float(float value)
creates a Float object for the primitive float argument.Float(String s)
creates a Float object that represents the floating-point value of type float represented by the string.
The following code creates three Float
objects.
Pay attention to the different type of values it is using.
public class Main {
public static void main(String[] args) {
Float float1 = new Float(0.0d);
Float float2 = new Float(0.1F);
Float float3 = new Float("0.2");
//from j a v a 2s .com
System.out.println(float1);
System.out.println(float2);
System.out.println(float3);
}
}
The output:
Infinite and Not A Number
isInfinite()
returns true if the value being tested is
infinitely large or small in magnitude.
isNaN( )
returns true if the value being tested is not a number.
Using the following methods you can find out if a float value is infinite value or it is not a number(NaN).
boolean isInfinite()
Returns true if this Float value is infinitely large in magnitude, false otherwise.static boolean isInfinite(float v)
Returns true if the specified number is infinitely large in magnitude, false otherwise.boolean isNaN()
Returns true if this Float value is a Not-a-Number (NaN), false otherwise.static boolean isNaN(float v)
Returns true if the specified number is a Not-a-Number (NaN) value, false otherwise.
Let's look at how to use the methods listed above.
public class Main {
public static void main(String[] args) {
Float floatObject2 = Float.valueOf((float)0.0/(float)(0.0));
System.out.println(floatObject2);//from j av a2s.c o m
System.out.println("isInfinite:"+floatObject2.isInfinite());
System.out.println("isNaN:"+floatObject2.isNaN());
}
}
The output:
More examples on isInfinite()
.
public class Main {
public static void main(String[] args) {
Float floatObject2 = Float.valueOf(1/(float)(0.0));
System.out.println(floatObject2);/*j a v a 2s.c om*/
System.out.println("isInfinite:"+floatObject2.isInfinite());
System.out.println("isNaN:"+floatObject2.isNaN());
}
}
The output:
Bit oriented calculation for float
static int floatToIntBits(float value)
returns a representation of the specified floating-point value according to the IEEE 754 floating-point "single format" bit layout.static int floatToRawIntBits(float value)
returns a representation of the specified floating-point value according to the IEEE 754 floating-point "single format" bit layout, preserving Not-a-Number (NaN) values.static float intBitsToFloat(int bits)
returns the float value corresponding to a given bit representation.
public class Main {
public static void main(String[] args) {
int i = Float.floatToIntBits(1.1F);
System.out.println(i);/*from j a v a 2s . com*/
}
}
The output:
Next chapter...
What you will learn in the next chapter:
- How to convert Float to byte, double, float, int, long and short
- How to convert float value to Hex String value
- How to convert float value to String value
- How to convert string value to float value