float Java Keyword with Examples

The float keyword is used to declare float primitive variable. A float variable may store a single−precision floating point value. The type float specifies a single-precision value that uses 32 bits of storage. Single precision is faster on some processors and takes half as much space as double precision, but will become imprecise when the values are either very large or very small.

The Float wrapper class is used to represent float primitive variable as an object in Java.

float Java Keyword Examples

Syntax
float <variable-name> = <float-value>;
Example:
float price = 19.95;
The float keyword can be used to the declared return type of a method as well.
public float getBalance() {
    return balanceAmount;
}
The Java wrapper class associated with the float data type is called Float that is defined in java.lang package i.e. java.lang.Float.
float f = 65.55;
Float fRef = f;

Remarks

The following rules apply to float keyword's use:
  • Floating point literals in Java always default to double−precision. To specify a single−precision literal value, follow the number with f or F, as in 0.01f.
  • In Java, float keyword is used to store the 32 bit float primitive types.
  • Java floating point numbers can represent infinity and NaN (not a number). The Float wrapper class defines the constants MIN_VALUE, MAX_VALUE, NEGATIVE_INFINITY, POSITIVE_INFINITY and NaN.

Free Spring Boot Tutorial | Full In-depth Course | Learn Spring Boot in 10 Hours


Watch this course on YouTube at Spring Boot Tutorial | Fee 10 Hours Full Course

Comments