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)

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.

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

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.

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.

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

Pratik said:   8 years ago
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.

So, the option 3 is also right.

NARENDRA said:   10 years ago
1.4 means java 2 platform 1.4 here principle production are J2SDK 1.4, J2SRE 1.4.

Code name is Merlin, released on Feb 6, 2002 editions J2ME, J2SE, J2EE.

Present java 7 version running JDK 1.7.

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.


Post your comments here:

Your comments will be displayed after verification.