Once you click post assignment, learners in the selected class will immediately be able to see and complete this assignment
Back to Classroom
Float
Java Variables Course
Engineers From These Top Companies and Universities Trust EXLskills
1M+ Professionals | 100+ Institutions
This is the EXLskills free and open-source Java Variables Micro Course. It guides learners via explanation, demonstration, and thorough practice, from no more than a basic understanding of Java, to a moderate level of understanding regarding Java variables and operators.
Is this course FREE?
Yes, this a 100% free course that you can contribute to on GitHub here!
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.
FloatDataTypeExample.java
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) {
System.out.println(exampleVariableOne);
System.out.println(exampleVariableTwo);
System.out.println(exampleVariableThree);
}
}
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.