|
|
|
1234.5 1 decimal digits after the decimal point 0.087 3 decimal digits after the decimal point 3.1415 5 decimal digits after the decimal point |
0.31415e1 (= 0.31415×101 = 3.1415) 31.415e-1 (= 31.415×10-1 = 3.1415) |
|
The floating point number representation will be discussed in detail in the CS255 course.
We will discuss the double precision kind first.
(We discuss double precision floating point variable first to avoid using the casting operation - casting will be explained later)
|
This kind of variable is commonly used in scientific computations.
A calculator uses double precision floating point variables.
|
BTW, that's how you perceive a double typed variable.
Inside the computer, the number is represented with bits.
A double typed variable looks something like this in reality:
The 64 bits encodes a floating point number
The encoding method used is called the IEEE 754 Standard - See: click here
Note to lecturer:
|
Also, you must use the correct syntax construct to write a variable definition
double NameOfVariable ; |
Notes:
|
public class Var01 { public static void main(String[] args) { double x; // Define floating point variable with name "x" System.out.println("Hello Class"); System.out.println(" The variable x contains this number:"); System.out.println(x); // Print variable "x" } } |
Notes:
|
A (large) portion of the RAM memory will remain unused
double x; |
is the following:
|
|
How to run the program:
|
Output: (Compile error)
Var01.java:11: variable x might not have been initialized System.out.println(x); // Print the content in variable "x" ^ 1 error |
Meaning:
|
|
|
VariableName = Expression ; |
Notes:
|
public class Var02 { public static void main(String[] args) { double x; // Define floating point variable with name "x" x = 0.31415e1; // Assign 3.1415 to x System.out.println("Hello Class"); System.out.println(" The variable x contains this number:"); System.out.println(x); // Print variable "x" } } |
Notes:
|
|
How to run the program:
|
Output:
Hello Class The variable x contains this number: 3.1415 |
double varName = initialValue ; |
Example:
public class Var03 { public static void main(String[] args) { double x = 0.31415e1; // Define an initialized variable System.out.println("Hello Class"); System.out.println(" The variable x contains this number:"); System.out.println(x); // Print variable "x" } } |
Example: defining 3 variables
public class Var04 { public static void main(String[] args) { double x = 0.31415e1; // <----- Focus here double y; double z = 2.71828; y = 1.0; System.out.println(x); // Print variable "x" System.out.println(y); // Print variable "y" System.out.println(z); // Print variable "z" } } |
Example: defines that same 3 variables with 1 clause
public class Var04 { public static void main(String[] args) { double x = 0.31415e1, y, z = 2.71828; // <----- Focus here y = 1.0; System.out.println(x); // Print variable "x" System.out.println(y); // Print variable "y" System.out.println(z); // Print variable "z" } } |
Notice the comma (,) separating the variable names.