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 1 of 2.

Radistao said:   10 years ago
From Java documentation:

https://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html.

>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.

From the task:

A. 1 (byte) and 3 (char)
C. 3 (char) and 5 (Short)

BOTH ARE CORRECT!!!
(2)

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

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");
}

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. !

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.

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.

BrutherJ said:   1 decade ago
What Pearl said?

Balla said:   1 decade ago
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.

Amaziane said:   1 decade ago
After java 5 we can use the wrappers type in switches control structure like this example :

Short S=40;

switch(S)
{
case 1:
System.out.println("12");
case 40:
System.out.println("40");
default:
System.out.println("Default");
}

This example will print :40.

Default.

Naresh said:   1 decade ago
Until 1.4v the only allowed arguments types for the switch statement are byte, short, int &char.

But 1.5v on wards corresponding wrapper classes& enum type is also allowed.

From 1.7v on wards String type is also allowed as argument to switch.


Post your comments here:

Your comments will be displayed after verification.