The float keyword is a data type in Java, which is used for floating point values. The range for float data type is 3.4e-038
to 3.4e+038
. This range is in scientific notation, the notation value 3.4e+038
is equivalent to 34 followed by 37 zeroes (see the example at the end of this post to learn how this notation works).
public class JavaExample { public static void main(String[] args) { float num = 45.789f; //float value ends with f System.out.println(num); } }
Output:
45.789
Note: For larger values than the higher range of float and lower values than the lower range of float, use the double data type.
Example of float keyword
Let’s see how scientific notation works. This will help you understand the range of float data type, which is mentioned in this notation at the starting of this article.
public class JavaExample { public static void main(String[] args) { //scientific notation values float num = 15.55e+03f; float num2 = 15.55e-03f; System.out.println(num); System.out.println(num2); } }
Output:
15550.0 0.01555