OneBite.Dev - Coding blog in a bite size

declare a float number in java

Code snippet on how to declare a float number in java

  float num = 1.22f;

This code creates a float variable called “num” and assigns a value of 1.22 to it. A float is a data type that can store decimals, and this code specifically creates a single-precision float, meaning it stores numbers up to four digits of precision. In this code, the “f” at the end of the number is important and tells the compiler that the data type should be a float, rather than a double (the compiler would otherwise assume it is a double by default, because 1.22 does not have a suffix). A float is a primitive data type in Java, meaning it is not an object like a String or an array.

java