Wednesday 20 July 2022

Why to do explicit type casting from double to float conversion?

Here is the scenario, consider the following statement.

byte b1 = 10;

 

literal '5' is of type int and can be fit into a variable of type byte. In this example compiler will do implicit type casting from int to byte.

 

Now consider the below statement.

float fl = 10.1;

 

literal '10.0' is of type double, and can be fit into a variable of type float, but you will get a compiler error why?

 

Let’s talk about int to byte conversion first

byte b1 = 10;

In the above example, java compiler knows that the value 10 is entirely fit into a byte variable (byte can store from -128 to 127), there is no loss of information here.

 


NumberConversion1.java

public class NumberConversion1 {
    
    public static void main(String[] args) {
        byte b1 = 10;
        
        System.out.println("b1 : " + b1);
    }

}

Compile and run above application.

$javac NumberConversion1.java 
$
$java NumberConversion1
b1 : 10

Let’s consider below statement.

byte b1 = 128;

As 128 is not fit into a byte variable (-128 to 127), there is a loss of information here. When you try to compile the code, you will get compiler error.

 

NumberConversion2.java

public class NumberConversion2 {
    
    public static void main(String[] args) {
        byte b1 = 128;
        
        System.out.println("b1 : " + b1);
    }

}

Try to compile above program, you will get the following error message.

$javac NumberConversion2.java 
NumberConversion2.java:4: error: incompatible types: possible lossy conversion from int to byte
        byte b1 = 128;
                  ^
1 error

Same is the case with following statement also.

float f1 = 10.1;

10.1 is a double which uses a 64-bit value and approximates 10.1 more accurately than 10.1f which only uses a 32-bit to store the data. Since there is a possible lossy conversion here, compiler throw the error.

 

NumberConversion3.java

public class NumberConversion3 {
    
    public static void main(String[] args) {
        float f1 = 10.1;
        
        System.out.println("f1 : " + f1);
    }

}

Try to compile above program, you will get below error.

$javac NumberConversion3.java 
NumberConversion3.java:4: error: incompatible types: possible lossy conversion from double to float
        float f1 = 10.1;
                   ^
1 error

10.1 is not same as 10.1f

Since 10.1 is a double which uses a 64-bit value and approximates 10.1 more accurately than 10.1f which only uses a 32-bit. Since there is a possible lossy conversion here, compiler throw the error.

 

Let’s confirm the same with below example.

 

NumberConversion4.java

public class NumberConversion4 {

    public static void main(String[] args) {
        if (10.1f == 10.1) {
            System.out.println("Both are same");
        } else {
            System.out.println("Both are not same");
        }
        
        System.out.println("(10.1 - 10.1f) = " + (10.1 - 10.1f));

    }

}

Output

Both are not same
(10.1 - 10.1f) = -3.8146972691777137E-7

Use explicit casting to convert a double to float value, here we are conveying to the compiler that I am agreeing to the possible loss of information.

float f1 = (float)10.1;

 

You may like

Interview Questions

What happen to the threads when main method complete execution

Why System.out.println() is not throwing NullPointerException on null references?

Why should we restrict direct access to instance properties?

Closeable vs AutoCloseable in Java

What is the behaviour of static final method in Java?

How to check two double values for equality?

No comments:

Post a Comment