Java Programming - Java.lang Class - Discussion

Discussion Forum : Java.lang Class - Finding the output (Q.No. 12)
12.
What will be the output of the program?
try 
{
    Float f1 = new Float("3.0");
    int x = f1.intValue();
    byte b = f1.byteValue();
    double d = f1.doubleValue();
    System.out.println(x + b + d);
}
catch (NumberFormatException e) /* Line 9 */
{
    System.out.println("bad number"); /* Line 11 */
}
9.0
bad number
Compilation fails on line 9.
Compilation fails on line 11.
Answer: Option
Explanation:

The xxxValue() methods convert any numeric wrapper object's value to any primitive type. When narrowing is necessary, significant bits are dropped and the results are difficult to calculate.

Discussion:
7 comments Page 1 of 1.

Subha said:   7 years ago
I think that,

x = int means it covert tat 3.0 as 3.
b = byte so that it takes byte as 0.
z = double means it takes the value as double which is 6.0
3 + 0 + 6.0 = 9.0.

Ejaz said:   10 years ago
Automatic conversion will make all the variables in print statement to double and then add it to become 9.0.
(1)

Bj venu said:   10 years ago
How we can add different data types?

Tanaji sen said:   1 decade ago
Someone please elaborate. I don't know the concept of wrapper class.

Vikas sahay said:   1 decade ago
But how can we add two different data type?

Suri said:   1 decade ago
Auto unboxing so they are automatticalyy convert into primitive.

Div said:   1 decade ago
How it s 9? in System.out.println + means concatenation and not (+ ie add).

Post your comments here:

Your comments will be displayed after verification.