Combination | # bits in memory cell | Capability |
---|---|---|
1 byte | 8 bits | 28 = 256 possible patterns |
2 bytes | 16 bits | 216 = 65536 possible patterns |
4 bytes | 32 bits | 232 = 4294967296 possible patterns |
|
|
|
|
|
|
|
double variableName ; |
The only difference is that we need to use a different keyword to denote single precision floating point variables
float variableName ; |
|
|
Usage: Enter a decimal number in the Decimal Floating-Point field and press Not Rounded
|
(float) --- convert to the single precision floating point representation (double) --- convert to the double precision floating point representation |
public class Casting01 { public static void main(String[] args) { float x; // Define single precision floating point double y; // Define double precision floating point x = 3.1415927f; // f denotes "float" y = (double) x; // **** convert to double representation System.out.print("Original single precision x = "); System.out.println(x); System.out.print("Converted double precision y = "); System.out.println(y); x = (float) y; // **** convert to float representation System.out.print("Re-converted single precision x = "); System.out.println(x); } } |
How to run the program:
|
Output:
Original single precision x = 3.1415927 Converted double precision y = 3.1415927410125732 Re-converted single precision x = 3.1415927 |
Notes:
|
|
are operators
Analogy:
Unary negation operator | Casting operator is a unary operator |
---|---|
−x (negates the value in variable x) | (float)x (converts the value in variable x) |
Priority level of casting operators:
Operator | Priority | Note |
---|---|---|
( .... ) | Highest | |
(float) (double) − | Higher | Unary operator, e.g.: (float) 3.0 |
* / | High | Binary operator, e.g.: 4 * 5 |
+ - | Lowest | Binary operator, e.g.: 4 + 5 |
|
public class Casting02 { public static void main(String[] args) { float x; // Define single precision floating point double y; // Define double precision floating point y = 3.14159265358979; // A "double" typed value x = (float) y; // **** convert to float representation System.out.print("Original double precision y = "); System.out.println(y); System.out.print("Converted single precision x = "); System.out.println(x); y = (double) x; // **** convert to double representation System.out.print("Re-converted double precision y = "); System.out.println(y); } } |
How to run the program:
|
Output:
Original double precision x = 3.14159265358979 Converted single precision y = 3.1415927 Re-converted double precision x = 3.1415927410125732 |
Notes:
|
Observation:
|
Explanation:
|
|
|
|
|
|
|
|
Right now, only two situations are relevant for our discussion:
|
|
public class Caveat1 { public static void main(String[] args) { double a; float b, c; a = 2.5f; b = 3.4f; c = a + b; // Compilation error !!! } } |
How to compile the program:
|
Can you see why the statement c = a + b will cause a compilation error ?
|
|
|
|
This general rule is applicable when we discuss other numerical data types (like int, short, etc).
float x; double y; y = x; ===> higher accuracy type = lower accuracy type 1. the float value in x is converted to a double 2. the (converted) value is assigned to y x = y; ===> lower accuracy type = higher accuracy type This assignment is NOT allowed (see rules above) (This is because the conversion is unsafe) x = (float) y; ===> 1. The casting operator (float) converts the double value into a float value 2. The (converted) float value is assigned to x y = x + y; ===> x + y 1. the float value is x is converted to a double 2. then + is performed on 2 double values y = double result 3. The result is double and can be assigned to y (because y is a double typed variable) x = x + y; ===> x + y 1. the float value is x is converted to a double 2. then + is performed on 2 double values x = double result 3. The result is double and cannot be assigned to x (because x is a float typed variable |