Java : Float with Examples
Float (Java SE 18 & JDK 18) API Examples.
You will find code examples on most Float methods.
Summary
// Compile error.
final var list = new ArrayList<float>();
// OK.
final var list = new ArrayList<Float>();
list.add(1.11f);
list.add(2.22f);
list.add(3.33f);
System.out.println(list); // [1.11, 2.22, 3.33]
Fields
static final int BYTES
System.out.println(Float.BYTES); // 4
static final int MAX_EXPONENT
System.out.println(Float.MAX_EXPONENT); // 127
static final float MAX_VALUE
// 3.4028235E38
System.out.println(Float.MAX_VALUE);
// 340282346638528859811704183484516925440
System.out.println(new BigDecimal(Float.MAX_VALUE).toPlainString());
static final int MIN_EXPONENT
System.out.println(Float.MIN_EXPONENT); // -126
static final float MIN_NORMAL
// 1.17549435E-38
System.out.println(Float.MIN_NORMAL);
// 0.0000000000000000000000000000000000000117549435082228750796
// 873653722224567781866555677208752150875170627841725945472717
// 28515625
System.out.println(new BigDecimal(Float.MIN_NORMAL).toPlainString());
static final float MIN_VALUE
// 1.4E-45
System.out.println(Float.MIN_VALUE);
// 0.0000000000000000000000000000000000000000000014012984643248
// 170709237295832899161312802619418765157717570682838897910826
// 8586060148663818836212158203125
System.out.println(new BigDecimal(Float.MIN_VALUE).toPlainString());
static final float NaN
System.out.println(Float.NaN); // NaN
static final float NEGATIVE_INFINITY
System.out.println(Float.NEGATIVE_INFINITY); // -Infinity
static final float POSITIVE_INFINITY
System.out.println(Float.POSITIVE_INFINITY); // Infinity
static final int SIZE
System.out.println(Float.SIZE); // 32
static final Class<Float> TYPE
System.out.println(Float.TYPE.getName()); // float
System.out.println(Float.TYPE.isPrimitive()); // true
Constructors
Float (double value)
Deprecated. Use valueOf(float f) instead.
Float (float value)
Deprecated. Use valueOf(float f) instead.
Float (String s)
Deprecated. Use valueOf(String s) instead.
Methods
byte byteValue ()
final byte value1 = Float.valueOf(100.0f).byteValue();
System.out.println(value1); // 100
final byte value2 = Float.valueOf(22.222f).byteValue();
System.out.println(value2); // 22
final byte value3 = Float.valueOf(9.99f).byteValue();
System.out.println(value3); // 9
System.out.println(Byte.MAX_VALUE); // 127
System.out.println(Byte.MIN_VALUE); // -128
final byte value1 = Float.valueOf(127.0f).byteValue();
System.out.println(value1); // 127
final byte value2 = Float.valueOf(128.0f).byteValue();
System.out.println(value2); // -128
final byte value3 = Float.valueOf(-128.0f).byteValue();
System.out.println(value3); // -128
final byte value4 = Float.valueOf(-129.0f).byteValue();
System.out.println(value4); // 127
final byte value1 = Float.valueOf(Float.NaN).byteValue();
System.out.println(value1); // 0
final byte value2 = Float.valueOf(Float.NEGATIVE_INFINITY).byteValue();
System.out.println(value2); // 0
final byte value3 = Float.valueOf(Float.POSITIVE_INFINITY).byteValue();
System.out.println(value3); // -1
static int compare (float f1, float f2)
System.out.println(Float.compare(1.23f, 4.56f)); // -1
System.out.println(Float.compare(4.56f, 1.23f)); // 1
System.out.println(Float.compare(1.23f, -1.23f)); // 1
System.out.println(Float.compare(-1.23f, 1.23f)); // -1
System.out.println(Float.compare(0.0f, -0.0f)); // 1
System.out.println(Float.compare(-0.0f, 0.0f)); // -1
System.out.println(Float.compare(Float.NaN, 1.23f)); // 1
System.out.println(Float.compare(Float.NEGATIVE_INFINITY, 1.23f)); // -1
System.out.println(Float.compare(Float.POSITIVE_INFINITY, 1.23f)); // 1
int compareTo (Float anotherFloat)
final var value1 = Float.valueOf(1.23f);
final var value2 = Float.valueOf(4.56f);
System.out.println(value1.compareTo(value2)); // -1
System.out.println(value2.compareTo(value1)); // 1
final var value1 = Float.valueOf(1.23f);
final var value2 = Float.valueOf(-1.23f);
System.out.println(value1.compareTo(value2)); // 1
System.out.println(value2.compareTo(value1)); // -1
final var value1 = Float.valueOf(0.0f);
final var value2 = Float.valueOf(-0.0f);
System.out.println(value1.compareTo(value2)); // 1
System.out.println(value2.compareTo(value1)); // -1
final var value = Float.valueOf(1.23f);
System.out.println(value.compareTo(Float.NaN)); // -1
System.out.println(value.compareTo(Float.NEGATIVE_INFINITY)); // 1
System.out.println(value.compareTo(Float.POSITIVE_INFINITY)); // -1
Optional<Float> describeConstable ()
final var ret = Float.valueOf(1.23f).describeConstable();
System.out.println(ret); // Optional[1.23]
double doubleValue ()
final var ret1 = Float.valueOf(1.23f).doubleValue();
System.out.println(ret1); // 1.2300000190734863
final var ret2 = Float.valueOf(-4.56f).doubleValue();
System.out.println(ret2); // -4.559999942779541
System.out.println(Float.valueOf(Float.NaN).doubleValue()); // NaN
System.out.println(Float.valueOf(Float.NEGATIVE_INFINITY).doubleValue()); // -Infinity
System.out.println(Float.valueOf(Float.POSITIVE_INFINITY).doubleValue()); // Infinity
boolean equals (Object obj)
final var value1 = Float.valueOf(1.23f);
final var value2 = Float.valueOf(1.23f);
System.out.println(value1.equals(value2)); // true
final var value1 = Float.valueOf(1.23f);
final var value2 = Float.valueOf(4.56f);
System.out.println(value1.equals(value2)); // false
// An example with rounding error.
final var value1 = Float.valueOf(0.05f);
final var value2 = Float.valueOf(0.02f + 0.03f);
System.out.println(value1.equals(value2)); // false
final var value1 = Float.valueOf(Float.NaN);
final var value2 = Float.valueOf(Float.NaN);
System.out.println(value1.floatValue() == value2.floatValue()); // false
System.out.println(value1.equals(value2)); // true
final var value1 = Float.valueOf(+0.0f);
final var value2 = Float.valueOf(-0.0f);
System.out.println(value1.floatValue() == value2.floatValue()); // true
System.out.println(value1.equals(value2)); // false
static int floatToIntBits (float value)
final var bits1 = Float.floatToIntBits(1.0f);
System.out.println(Integer.toHexString(bits1)); // 3f800000
final var bits2 = Float.floatToIntBits(-1.0f);
System.out.println(Integer.toHexString(bits2)); // bf800000
final var bits = Float.floatToIntBits(123.25f);
System.out.println(Integer.toHexString(bits)); // 42f68000
final var bits1 = Float.floatToIntBits(Float.NaN);
System.out.println(Integer.toHexString(bits1)); // 7fc00000
final var bits2 = Float.floatToIntBits(Float.NEGATIVE_INFINITY);
System.out.println(Integer.toHexString(bits2)); // ff800000
final var bits3 = Float.floatToIntBits(Float.POSITIVE_INFINITY);
System.out.println(Integer.toHexString(bits3)); // 7f800000
static int floatToRawIntBits (float value)
final var bits1 = Float.floatToRawIntBits(1.0f);
System.out.println(Integer.toHexString(bits1)); // 3f800000
final var bits2 = Float.floatToRawIntBits(-1.0f);
System.out.println(Integer.toHexString(bits2)); // bf800000
final var bits = Float.floatToRawIntBits(123.25f);
System.out.println(Integer.toHexString(bits)); // 42f68000
final var bits1 = Float.floatToRawIntBits(Float.NaN);
System.out.println(Integer.toHexString(bits1)); // 7fc00000
final var bits2 = Float.floatToRawIntBits(Float.NEGATIVE_INFINITY);
System.out.println(Integer.toHexString(bits2)); // ff800000
final var bits3 = Float.floatToRawIntBits(Float.POSITIVE_INFINITY);
System.out.println(Integer.toHexString(bits3)); // 7f800000
float floatValue ()
final float ret1 = Float.valueOf(1.23f).floatValue();
System.out.println(ret1); // 1.23
final float ret2 = Float.valueOf(-4.56f).floatValue();
System.out.println(ret2); // -4.56
System.out.println(Float.valueOf(Float.NaN).floatValue()); // NaN
System.out.println(Float.valueOf(Float.NEGATIVE_INFINITY).floatValue()); // -Infinity
System.out.println(Float.valueOf(Float.POSITIVE_INFINITY).floatValue()); // Infinity
int hashCode ()
System.out.println(Float.valueOf(1.23f).hashCode()); // 1067282596
System.out.println(Float.valueOf(-4.56f).hashCode()); // -1064178811
System.out.println(Float.valueOf(Float.MAX_VALUE).hashCode()); // 2139095039
System.out.println(Float.valueOf(Float.MIN_VALUE).hashCode()); // 1
System.out.println(Float.valueOf(Float.NaN).hashCode()); // 2143289344
System.out.println(Float.valueOf(Float.NEGATIVE_INFINITY).hashCode()); // -8388608
System.out.println(Float.valueOf(Float.POSITIVE_INFINITY).hashCode()); // 2139095040
System.out.println(Float.valueOf(+0.0f).hashCode()); // 0
System.out.println(Float.valueOf(-0.0f).hashCode()); // -2147483648
static int hashCode (float value)
System.out.println(Float.hashCode(1.23f)); // 1067282596
System.out.println(Float.hashCode(-4.56f)); // -1064178811
System.out.println(Float.hashCode(Float.MAX_VALUE)); // 2139095039
System.out.println(Float.hashCode(Float.MIN_VALUE)); // 1
System.out.println(Float.hashCode(Float.NaN)); // 2143289344
System.out.println(Float.hashCode(Float.NEGATIVE_INFINITY)); // -8388608
System.out.println(Float.hashCode(Float.POSITIVE_INFINITY)); // 2139095040
System.out.println(Float.hashCode(+0.0f)); // 0
System.out.println(Float.hashCode(-0.0f)); // -2147483648
static float intBitsToFloat (int bits)
System.out.println(Float.intBitsToFloat(0x3f800000)); // 1.0
System.out.println(Float.intBitsToFloat(0xbf800000)); // -1.0
System.out.println(Float.intBitsToFloat(0x42f68000)); // 123.25
System.out.println(Float.intBitsToFloat(0x7fc00000)); // NaN
System.out.println(Float.intBitsToFloat(0xff800000)); // -Infinity
System.out.println(Float.intBitsToFloat(0x7f800000)); // Infinity
int intValue ()
final int value1 = Float.valueOf(100.0f).intValue();
System.out.println(value1); // 100
final int value2 = Float.valueOf(22.222f).intValue();
System.out.println(value2); // 22
final int value3 = Float.valueOf(9.99f).intValue();
System.out.println(value3); // 9
System.out.println(Integer.MAX_VALUE); // 2147483647
System.out.println(Integer.MIN_VALUE); // -2147483648
final int value1 = Float.valueOf(2147483647.0f).intValue();
System.out.println(value1); // 2147483647
final int value2 = Float.valueOf(2147483648.0f).intValue();
System.out.println(value2); // 2147483647
final int value3 = Float.valueOf(-2147483648.0f).intValue();
System.out.println(value3); // -2147483648
final int value4 = Float.valueOf(-2147483649.0f).intValue();
System.out.println(value4); // -2147483648
final int value1 = Float.valueOf(Float.NaN).intValue();
System.out.println(value1); // 0
final int value2 = Float.valueOf(Float.NEGATIVE_INFINITY).intValue();
System.out.println(value2); // -2147483648
final int value3 = Float.valueOf(Float.POSITIVE_INFINITY).intValue();
System.out.println(value3); // 2147483647
static boolean isFinite (float f)
System.out.println(Float.isFinite(0.0f)); // true
System.out.println(Float.isFinite(1.23f)); // true
System.out.println(Float.isFinite(-4.56f)); // true
System.out.println(Float.isFinite(Float.NaN)); // false
System.out.println(Float.isFinite(Float.NEGATIVE_INFINITY)); // false
System.out.println(Float.isFinite(Float.POSITIVE_INFINITY)); // false
boolean isInfinite ()
System.out.println(Float.valueOf(0.0f).isInfinite()); // false
System.out.println(Float.valueOf(1.23f).isInfinite()); // false
System.out.println(Float.valueOf(-4.56f).isInfinite()); // false
System.out.println(Float.valueOf(Float.NaN).isInfinite()); // false
System.out.println(Float.valueOf(Float.NEGATIVE_INFINITY).isInfinite()); // true
System.out.println(Float.valueOf(Float.POSITIVE_INFINITY).isInfinite()); // true
static boolean isInfinite (float v)
System.out.println(Float.isInfinite(0.0f)); // false
System.out.println(Float.isInfinite(1.23f)); // false
System.out.println(Float.isInfinite(-4.56f)); // false
System.out.println(Float.isInfinite(Float.NaN)); // false
System.out.println(Float.isInfinite(Float.NEGATIVE_INFINITY)); // true
System.out.println(Float.isInfinite(Float.POSITIVE_INFINITY)); // true
boolean isNaN ()
System.out.println(Float.valueOf(0.0f).isNaN()); // false
System.out.println(Float.valueOf(1.23f).isNaN()); // false
System.out.println(Float.valueOf(-4.56f).isNaN()); // false
System.out.println(Float.valueOf(Float.NaN).isNaN()); // true
System.out.println(Float.valueOf(Float.NEGATIVE_INFINITY).isNaN()); // false
System.out.println(Float.valueOf(Float.POSITIVE_INFINITY).isNaN()); // false
static boolean isNaN (float v)
System.out.println(Float.isNaN(0.0f)); // false
System.out.println(Float.isNaN(1.23f)); // false
System.out.println(Float.isNaN(-4.56f)); // false
System.out.println(Float.isNaN(Float.NaN)); // true
System.out.println(Float.isNaN(Float.NEGATIVE_INFINITY)); // false
System.out.println(Float.isNaN(Float.POSITIVE_INFINITY)); // false
long longValue ()
final long value1 = Float.valueOf(100.0f).longValue();
System.out.println(value1); // 100
final long value2 = Float.valueOf(22.222f).longValue();
System.out.println(value2); // 22
final long value3 = Float.valueOf(9.99f).longValue();
System.out.println(value3); // 9
System.out.println(Long.MAX_VALUE); // 9223372036854775807
System.out.println(Long.MIN_VALUE); // -9223372036854775808
final long value1 = Float.valueOf(9223372036854775807.0f).longValue();
System.out.println(value1); // 9223372036854775807
final long value2 = Float.valueOf(9223372036854775808.0f).longValue();
System.out.println(value2); // 9223372036854775807
final long value3 = Float.valueOf(-9223372036854775808.0f).longValue();
System.out.println(value3); // -9223372036854775808
final long value4 = Float.valueOf(-9223372036854775809.0f).longValue();
System.out.println(value4); // -9223372036854775808
final long value1 = Float.valueOf(Float.NaN).longValue();
System.out.println(value1); // 0
final long value2 = Float.valueOf(Float.NEGATIVE_INFINITY).longValue();
System.out.println(value2); // -9223372036854775808
final long value3 = Float.valueOf(Float.POSITIVE_INFINITY).longValue();
System.out.println(value3); // 9223372036854775807
static float max (float a, float b)
System.out.println(Float.max(1.23f, 4.56f)); // 4.56
System.out.println(Float.max(-7.89f, 7.89f)); // 7.89
System.out.println(Float.max(1.23f, Float.NaN)); // NaN
System.out.println(Float.max(1.23f, Float.NEGATIVE_INFINITY)); // 1.23
System.out.println(Float.max(1.23f, Float.POSITIVE_INFINITY)); // Infinity
System.out.println(Float.max(+0.0f, -0.0f)); // 0.0
static float min (float a, float b)
System.out.println(Float.min(1.23f, 4.56f)); // 1.23
System.out.println(Float.min(-7.89f, 7.89f)); // -7.89
System.out.println(Float.min(1.23f, Float.NaN)); // NaN
System.out.println(Float.min(1.23f, Float.NEGATIVE_INFINITY)); // -Infinity
System.out.println(Float.min(1.23f, Float.POSITIVE_INFINITY)); // 1.23
System.out.println(Float.min(+0.0f, -0.0f)); // -0.0
static float parseFloat (String s)
final var ret1 = Float.parseFloat("1.23");
System.out.println(ret1); // 1.23
final var ret2 = Float.parseFloat("-4.56");
System.out.println(ret2); // -4.56
final var ret3 = Float.parseFloat("1.234e+3");
System.out.println(ret3); // 1234.0
final var ret4 = Float.parseFloat("1.234e-3");
System.out.println(ret4); // 0.001234
final var ret5 = Float.parseFloat("0xffffp-1");
System.out.println(ret5); // 32767.5
System.out.println(Float.toHexString(ret5)); // 0x1.fffep14
final var ret1 = Float.parseFloat("NaN");
System.out.println(ret1); // NaN
final var ret2 = Float.parseFloat("-Infinity");
System.out.println(ret2); // -Infinity
final var ret3 = Float.parseFloat("Infinity");
System.out.println(ret3); // Infinity
Float resolveConstantDesc (MethodHandles.Lookup lookup)
final var value = Float.valueOf(1.23f);
final var ret = value.resolveConstantDesc(MethodHandles.lookup());
System.out.println(ret); // 1.23
short shortValue ()
final var value1 = Float.valueOf(100.0f).shortValue();
System.out.println(value1); // 100
final var value2 = Float.valueOf(22.222f).shortValue();
System.out.println(value2); // 22
final var value3 = Float.valueOf(9.99f).shortValue();
System.out.println(value3); // 9
System.out.println(Short.MAX_VALUE); // 32767
System.out.println(Short.MIN_VALUE); // -32768
final var value1 = Float.valueOf(32767.0f).shortValue();
System.out.println(value1); // 32767
final var value2 = Float.valueOf(32768.0f).shortValue();
System.out.println(value2); // -32768
final var value3 = Float.valueOf(-32768.0f).shortValue();
System.out.println(value3); // -32768
final var value4 = Float.valueOf(-32769.0f).shortValue();
System.out.println(value4); // 32767
final var value1 = Float.valueOf(Float.NaN).shortValue();
System.out.println(value1); // 0
final var value2 = Float.valueOf(Float.NEGATIVE_INFINITY).shortValue();
System.out.println(value2); // 0
final var value3 = Float.valueOf(Float.POSITIVE_INFINITY).shortValue();
System.out.println(value3); // -1
static float sum (float a, float b)
System.out.println(Float.sum(1.0f, 5.0f)); // 6.0
System.out.println(Float.sum(1.0f, -5.0f)); // -4.0
System.out.println(Float.sum(1.234f, Float.NaN)); // NaN
System.out.println(Float.sum(1.234f, Float.NEGATIVE_INFINITY)); // -Infinity
System.out.println(Float.sum(1.234f, Float.POSITIVE_INFINITY)); // Infinity
// An example with rounding error.
System.out.println(Float.sum(0.02f, 0.03f)); // 0.049999997
static String toHexString (float f)
System.out.println(Float.toHexString(1.0f)); // 0x1.0p0
System.out.println(Float.toHexString(-1.0f)); // -0x1.0p0
System.out.println(Float.toHexString(2.0f)); // 0x1.0p1
System.out.println(Float.toHexString(3.0f)); // 0x1.8p1
System.out.println(Float.toHexString(0.5f)); // 0x1.0p-1
System.out.println(Float.toHexString(0.25f)); // 0x1.0p-2
System.out.println(Float.toHexString(Float.MAX_VALUE)); // 0x1.fffffep127
System.out.println(Float.toHexString(Float.MIN_VALUE)); // 0x0.000002p-126
System.out.println(Float.toHexString(Float.NaN)); // NaN
System.out.println(Float.toHexString(Float.NEGATIVE_INFINITY)); // -Infinity
System.out.println(Float.toHexString(Float.POSITIVE_INFINITY)); // Infinity
String toString ()
System.out.println(Float.valueOf(1.23f).toString()); // 1.23
System.out.println(Float.valueOf(-4.56f).toString()); // -4.56
System.out.println(Float.valueOf(1234567890.0f).toString()); // 1.23456794E9
System.out.println(Float.valueOf(1.23e-10f).toString()); // 1.23E-10
System.out.println(Float.valueOf(0.0f).toString()); // 0.0
System.out.println(Float.valueOf(-0.0f).toString()); // -0.0
System.out.println(Float.valueOf(Float.MAX_VALUE).toString()); // 3.4028235E38
System.out.println(Float.valueOf(Float.MIN_VALUE).toString()); // 1.4E-45
System.out.println(Float.valueOf(Float.NaN).toString()); // NaN
System.out.println(Float.valueOf(Float.NEGATIVE_INFINITY).toString()); // -Infinity
System.out.println(Float.valueOf(Float.POSITIVE_INFINITY).toString()); // Infinity
static String toString (float f)
System.out.println(Float.toString(1.23f)); // 1.23
System.out.println(Float.toString(-4.56f)); // -4.56
System.out.println(Float.toString(1234567890.0f)); // 1.23456794E9
System.out.println(Float.toString(1.23e-10f)); // 1.23E-10
System.out.println(Float.toString(0.0f)); // 0.0
System.out.println(Float.toString(-0.0f)); // -0.0
System.out.println(Float.toString(Float.MAX_VALUE)); // 3.4028235E38
System.out.println(Float.toString(Float.MIN_VALUE)); // 1.4E-45
System.out.println(Float.toString(Float.NaN)); // NaN
System.out.println(Float.toString(Float.NEGATIVE_INFINITY)); // -Infinity
System.out.println(Float.toString(Float.POSITIVE_INFINITY)); // Infinity
static Float valueOf (float f)
final var value1 = Float.valueOf(1.23f);
System.out.println(value1); // 1.23
final var value2 = Float.valueOf(-4.56f);
System.out.println(value2); // -4.56
final var value3 = Float.valueOf(1234567890.0f);
System.out.println(value3); // 1.23456794E9
final var value4 = Float.valueOf(1.23e-10f);
System.out.println(value4); // 1.23E-10
final var value5 = Float.valueOf(Float.MAX_VALUE);
System.out.println(value5); // 3.4028235E38
final var value6 = Float.valueOf(Float.MIN_VALUE);
System.out.println(value6); // 1.4E-45
static Float valueOf (String s)
final var value1 = Float.valueOf("1.23");
System.out.println(value1); // 1.23
final var value2 = Float.valueOf("-4.56");
System.out.println(value2); // -4.56
final var value3 = Float.valueOf("1.234e+3");
System.out.println(value3); // 1234.0
final var value4 = Float.valueOf("1.234e-3");
System.out.println(value4); // 0.001234
final var value5 = Float.valueOf("0xffffp-1");
System.out.println(value5); // 32767.5
System.out.println(Float.toHexString(value5)); // 0x1.fffep14
final var value1 = Float.valueOf("NaN");
System.out.println(value1); // NaN
final var value2 = Float.valueOf("-Infinity");
System.out.println(value2); // -Infinity
final var value3 = Float.valueOf("Infinity");
System.out.println(value3); // Infinity