After getting to know the integer data type, this time we'll look at another numeric data type, float.
About the Float Data Type
Floating Points, or usually float, is a standard data type that holds decimal numbers. If integer only holds whole numbers, float also holds fractional ones. For example: 1.5, 2.2, 3.14, and so on.
Float Sub Types
This data type splits into 2 based on precision — how many digits sit after the dot, namely:
- float: a 32-bit (4 byte) data type that holds 6 to 7 digits after the dot. The primitive type is
float, while the object form isFloat. - double: a 64-bit (8 byte) data type that holds 15 to 16 digits after the dot. The primitive type is
double, while the object form isDouble.
As you can see, the higher the precision, the bigger the size. Since computers today are more capable, using double doesn't make a significant difference compared with float. Still, saving memory by using float whenever possible is a good idea.
Float Literals
A float literal is any real number. That covers positive, negative, whole and fractional numbers, and zero.
Unlike decimal numbers in Indonesia, which are separated by a comma (,), decimal numbers in Java use a dot (.).
For example:
double PI = 3.141592653589793; // 15 digits after the dot
float tiga = 3; // whole number
double empat = 4.0; // whole number
Using Letter Suffixes
You can also add the letter f or d to mark a number as float or double. This is strongly recommended as a type marker for a literal.
For example:
float pi = 3.141592f; // the `f` suffix is required
double PI = 3.141592653589793d; // the `d` suffix is optional
float tiga = 3f; // the `f` suffix is optional
float empat = 4.0f; // the `f` suffix is required
double lima = 5d; // the `d` suffix is optional
By default Java reads decimal numbers as the double data type, which is why the f suffix is needed to mark a literal as float. Without it the program errors out, because Java can't convert a double to a float automatically.
When doing arithmetic, division especially, this marker matters a lot because it can change the result. We'll cover that further in the next section.
Using Scientific Notation
A float literal can also be written in scientific notation. In case you forgot, scientific notation uses multiplication by powers of 10. More on that here.
Java uses the e notation to write the literal. For example like this:
double seribu = 1e3; // 1 * 10^3 = 1000
float duaJuta = 2e6f; // 2 * 10^6 = 2000000
double bilanganKecil = 7e-10; // 0.0000000007
float bilanganKecil2 = 4e-5f; // 0.00004
Operations on the Float Data Type
Every operation that works on integer works on float too. There's arithmetic, arithmetic assignment, and the unary operator. More on that in the earlier discussion.
The division operation is probably what makes it a little different. As we touched on earlier, adding the letter f or d can change the result.
Let's look at an example.
public class TipeDataFloat{
public static void main(String[] args) {
float a = 22;
float b = 7;
float hasil = a / b;
System.out.printf("hasil dari %.2f / %.2f adalah %.2f", a, b, hasil);
}
}
Careful! the letter
fabove isn't part of a float literal, it's part of the string template for printing a float. More on that here
If you run it, the result looks like this:
hasil dari 22.00 / 7.00 adalah 3.14
So far so good.
Now let's fill the hasil variable with an integer literal division. We'll change the code above to this:
public class TipeDataFloat{
public static void main(String[] args) {
float hasil = 22 / 7;
System.out.printf("hasil dari 22 / 7 adalah %.2f", hasil);
}
}
Now run that code and look at the result. The calculation surely comes out wrong. Instead of printing 3.14 it prints 3.00.
hasil dari 22 / 7 adalah 3.00
So why does that happen?
Because we use integer literals for the division. Remember, division on integers is always rounded and can never produce a decimal number.
As a result, the integer division of 22 and 7, which gives the whole number 3, goes into the hasil variable. So what lands in hasil is purely the integer result, regardless of the variable's data type.
To fix this we can use a float literal by adding the letter f after the number. So the code becomes like this:
public class TipeDataFloat{
public static void main(String[] args) {
float hasil = 22f / 7f;
// you can also write `22f / 7` or `22 / 7f`
// but for consistency, put `f` on both
System.out.printf("hasil dari 10 / 8 adalah %.2f", hasil);
}
}
Next Up
Now that the 2 numeric data types in Java are done (integer and float), next we'll move to data types that hold letters and characters.
Next we'll talk about the 'char' data type in the Java language.
Hope it helps, happy coding 👋