Java Programming - Flow Control - Discussion

Discussion Forum : Flow Control - Finding the output (Q.No. 13)
13.
What will be the output of the program?
Float f = new Float("12"); 
switch (f) 
{
    case 12: System.out.println("Twelve"); 
    case 0: System.out.println("Zero"); 
    default: System.out.println("Default"); 
}
Zero
Twelve
Default
Compilation fails
Answer: Option
Explanation:

The switch statement can only be supported by integers or variables more "narrow" than an integer i.e. byte, char, short. Here a Float wrapper object is used and so the compilation fails.

Discussion:
5 comments Page 1 of 1.

Mayur said:   1 decade ago
Float f = new Float("12"); is this vaild?

Neva said:   1 decade ago
Switch Statement allows "String" since Java 7. :)

Moni said:   1 decade ago
Switch statement can take Wrapper object also,but compilation fails because of Float Wrapper class.

We can't take Float, Double Wrapper classes like float primitive data type float double.

Praveen PS said:   1 decade ago
From java 7 onward we can use string inside switch.

Mitul said:   10 years ago
Argument of float is string & value provided by switch is integer. That's why it is throwing error. Please correct me if I understood wrongly.

Post your comments here:

Your comments will be displayed after verification.