In Java, a floating-point literal is a numeric value with a decimal point that represents a value of the float
or double
data type.
Here are some examples of floating-point literals in Java:
public class Main { public static void main(String[] args) { float value1 = 3.14f; // The "f" suffix indicates that the value is a float double value2 = 3.14; // The default data type for decimal values is double double value3 = 6.022e+23; // The "e" notation indicates scientific notation } }
In the first example, the value 3.14
is assigned to a float
variable. The f
suffix indicates that the value is a float
literal and should be treated as a float
value.
In the second example, the value 3.14
is assigned to a double
variable. The default data type for decimal values in Java is double
, so no suffix is needed.
In the third example, the value 6.022e+23
is assigned to a double
variable. The e
notation indicates scientific notation, and the +23
exponent indicates that the value should be multiplied by 10 to the power of 23.
You can use floating-point literals to represent decimal values in your Java programs. Just be aware that the float
data type has less precision than the double
data type, so you may want to use the double
data type if you need greater precision.