DUET PG Score Card 2021
NHAI Recruitment 2021
Bihar Board 12th Dummy Admit Card 2022
RBI Assistant Notification 2021
Jee Advanced 2021 Result
NIACL Assistant Recruitment 2021
SBI PO Online Form 2021

Learn how type widening works when using float

Using Float: The below statement declares a float and a double as x and y initialized to 3.4f and 5.1d respectively.

float x = 3.4f;
double y = 5.1d;

However, when we try to perform an addition operation, such as given below, we get a compilation error : Type mismatch: cannot convert from double to float.

float sum = x + y;

When a binary operator (like, *, / ,%, +, etc. ) is being used, if at least one operand is of type double and the other float then the operation is carried out by automatically widening the float operand also to double.
And for all other numeric types (like byte, short, char, int and long) the operands are automatically widened to float and the result is also a float.

In our case, since y is of type double and x is of type float, x is widened to double. And the resultant which is also a double is assigned to sum (which is of type float).
A compile-time error occurs while assigning the result (of type double) to sum. To fix it, we will have to explicitly type cast the result to a float, as shown below.

float sum = (float)(x + y);

Applying the above understanding identify and correct the error so that the correct output is produced.

Note: Please don’t change the package name.

package q10858;
public class FloatDemo {
    public static void main(String[] args) {
            float x = 1.5 f;
            double y = 1.1 d;
            float sum = (float)(x + y);
            System.out.println("sum = " + sum);
            // should print 2.6 

Candidates having any kind of query so he/she can ask in our comment section. Our Panel will b reverting back as soon as possible. Thank You. For more details visit our website (https://speedjobalert.com/)

Leave a Comment