Java Programming - Flow Control - Discussion

Discussion Forum : Flow Control - General Questions (Q.No. 2)
2.
switch(x) 
{ 
    default:  
        System.out.println("Hello"); 
}
Which two are acceptable types for x?
  1. byte
  2. long
  3. char
  4. float
  5. Short
  6. Long
1 and 3
2 and 4
3 and 5
4 and 6
Answer: Option
Explanation:

Switch statements are based on integer expressions and since both bytes and chars can implicitly be widened to an integer, these can also be used. Also shorts can be used. Short and Long are wrapper classes and reference types can not be used as variables.

Discussion:
16 comments Page 2 of 2.

BrutherJ said:   1 decade ago
What Pearl said?

Pearl said:   1 decade ago
Now, Java supports automatically convert Wrapper class data to primitive type data. So, Character, Byte, Short and Integer could be used in Switch.

Basil Bibi said:   1 decade ago
A switch works with the byte, short, char, and int primitive data types. It also works with enumerated types (discussed in Enum Types), the String class, and a few special classes that wrap certain primitive types: Character, Byte, Short, and Integer.

Vishal said:   1 decade ago
Here Short and Long are wrapper classes. So we can't use with switch.

Float type variable are not permitted with switch case. !

Nd char n short given in options. So it is correct. !

Cause of range of byte and char. !

Arvind said:   1 decade ago
In the explanation it is provided that Short and Long are wrapper classes and reference types can not be used as variables.

However, Byte which is a wrapper class too works for x and switch... why does wrapper classes block does not apply on Byte?

Byte x = 'L';
switch(x)
{
default:
System.out.println("Hello");
}

Saurabh said:   1 decade ago
About byte data type in java. ?


Post your comments here:

Your comments will be displayed after verification.