Primitive Data Types


🙋 Need help? Ask an expert now!

Let's explore the next primitive data type, float, or single-precision floating point. Floating point literals have a decimal point, but no commas that function as thousand separators. Please take a look at the code below to see how float variables are declared and used.

package exlcode;

public class FloatDataTypeExample {

  // float can be any rational number between -3.4E+38f and 3.4E+38f
  public static float exampleVariableOne = 0.15f;
  public static float exampleVariableTwo = -0.15f;
  public static float exampleVariableThree = 3.4E+10f;

  public static void main(String[] args) {

float variables can be represented with a lowercase 'f' OR uppercase 'F' at the end, which asks the program specifically for a single-precision float literal which deals with a 32 bit floating point. Even without the 'f' or 'F' at the end the program, it assumes a float is declared and initialized.

This datatype is used when dealing with decimals. As said in the comments of the code above, float can take on a range of values. However, double is preferred when we want a more precise answer as double deals with 64 bit numbers. The float primitive data type is NOT used for precise values like currency because the data type is sometimes approximate and rounded.

Edit Me on GitHub!

Application Question

Which one of these is a valid float declaration?