Floating point literals represent fractional numbers like `3.14159`

, `-14.08`

, `42.0`

, `675.238`

, and so on. **Floating point literals can be written in either standard or scientific notation.** The numbers `1.5E4`

and `6.667e-11`

are written in scientific notation.

Standard notation is the usual way we write fractional numbers. We first write the whole number part followed by the decimal point followed by the fractional component like this — `15.08`

In scientific notation, the floating-point literal consists of 2 parts — the mantissa and the exponent. The mantissa must be an integer or a floating-point literal in standard notation. It is followed by the letter e and then the exponent is written. The case of letter e doesn’t matter, it can be written in either uppercase or lowercase. Exponent must be an integer. Both the mantissa and the exponent can be positive or negative. The exponent indicates the power of 10 by which the mantissa is to be multiplied and E represents the number 10.

Here are a few more examples to help you understand this better:

Standard Notation | Mantissa | Exponent | Scientific Notation |
---|---|---|---|

15000.0 | 1.5 | 4 | 1.5E4 |

0.00000000006667 | 6.667 | -11 | 6.667e-11 |

-2.1023 | -210 | -2 | -210.23e-2 |

Let’s look at a program in BlueJ to see some examples of Floating point literals.

```
public class FloatingPointLiteralDemo
{
public void demoFPLiteral() {
float pi = 3.14159f;
System.out.println("Value of pi is " + pi);
float scientificEx1 = 5.3E-4f; //0.00053 in scientific notation
System.out.println("Value of scientificEx1 is " + scientificEx1);
double scientificEx2 = 6.667e-11; //0.00000000006667 in scientific notation
System.out.println("Value of scientificEx2 is " + scientificEx2);
double literalWith_ = 93_2_3_4.9_5_7;
System.out.println("Value of literalWith_ is " + literalWith_);
}
}
```

**When you write a floating-point literal in Java, by default Java treats it as a double data type value.** To specify a float literal, you must append a `f`

or `F`

to the constant like this — `float pi = 3.14159f;`

. The case of `f`

doesn’t matter, it can be either small or capital. Now, if we don’t specify `f`

here then the compiler will give an error. When I don’t specify `f`

, Java treats `3.14159`

as a `double`

value. The variable `pi`

is of `float`

type. The range of `float`

is less than that of `double`

. So when I am assigning `3.14159`

to `pi`

, I am actually asking the compiler to convert a value of type `double`

into a value of type `float`

. The compiler knows that this might lead to data loss if the double value is outside the range of float. Hence it doesn’t allow any such implicit conversions and flags this error. When we specify `f`

at the end of `3.14159`

, we explicitly tell the compiler to treat it as a `float`

type not `double`

type. Now we are assigning a `float`

type to a `float`

type. No type conversion is happening so the compiler is happy.

Here is the output of the program:

Did you notice this line in the program?

`double literalWith_ = 93_2_3_4.9_5_7;`

Just like in integer literals, we can use underscores to separate the digits in floating point literals too. As we can see in the output, the underscores are discarded by the compiler.

## Boolean Literals

Boolean literals are straightforward. **A boolean literal can take only one of the two boolean values represented by the words true or false.** Boolean literals can only be assigned to variables declared as boolean or used in expressions with boolean operators.

For those of you coming from a C++ background please note that in Java the boolean literals true and false do not convert to any numerical representation. This is different from C++. **In Java, false literal is not equal to 0 and true literal does not have any numeric value other than 0. They are purely boolean literals true and false.**