Floating-point Literals in java
Java Programming Language / Variables in java
1769
This is a Java program that demonstrates the use of double literals. In Java, a double literal is a number with a decimal point, without any suffix or with the suffix D
or d
to indicate that it is a double
type.
The program declares three double
variables named double_value1
, double_value2
, and double_value3
, and assigns them different double
literals as values. Specifically:
double_value1
is assigned the value89.0D
, which represents the decimal value 89.0 as adouble
type.double_value2
is assigned the value89.0d
, which is another way to represent the decimal value 89.0 as adouble
type.double_value3
is assigned the value89.0
, which is a decimal literal that represents the decimal value 89.0 as adouble
type by default.
The program then prints the values of these variables to the console using the System.out.println
method. When run, the program will output the values of double_value1
, double_value2
, and double_value3
, which are all 89.0.
Program:
//Java literals are fixed or constant values in a program's source code. public class DoubleLiteral { public static void main(String[] a) { double double_value1 = 89.0D; //OK double double_value2 = 89.0d; //OK double double_value3 = 89.0; //OK, by default floating point literal is double System.out.println(double_value1); System.out.println(double_value2); System.out.println(double_value3); } } /* Floating-point literals in Java default to double precision. To specify a float literal, you must append an F or f to the constant. You can also explicitly specify a double literal by appending a D or d. float f = 89.0; // Type mismatch: cannot convert from double to float float ff = 89.0f; //OK double dou = 89.0D; //OK double doub = 89.0d; //OK double doubl = 89.0; //OK, by default floating point literal is double */
Output:
89.0 89.0 89.0 Press any key to continue . . .
Explanation:
In summary, this program demonstrates how to use double literals to assign values to double
type variables in Java, and also shows that the D
or d
suffix is optional when initializing a double
variable with a literal value.
This Particular section is dedicated to Programs only. If you want learn more about Java Programming Language. Then you can visit below links to get more depth on this subject.