Java Programming - Flow Control - Discussion

Discussion :: 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

[A]. 1 and 3
[B]. 2 and 4
[C]. 3 and 5
[D]. 4 and 6

Answer: Option A

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.


Saurabh said: (Jul 6, 2011)  
About byte data type in java. ?

Arvind said: (Jun 9, 2012)  
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: (Nov 2, 2012)  
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: (May 15, 2013)  
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: (Feb 13, 2014)  
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: (Mar 1, 2014)  
What Pearl said?

Balla said: (Jun 30, 2014)  
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: (Jul 2, 2014)  
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: (Sep 18, 2014)  
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.

Ajay said: (Jun 18, 2015)  
What is the meaning of 1.4v?

Thanks in advance.

Narendra said: (Oct 9, 2015)  
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.

Radistao said: (Dec 5, 2015)  
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!!!

Anish Kumar said: (Aug 24, 2016)  
From 1.5 version all wrapper class is allowed e.g Integer, Short.
From 1.7 version string is also allowed.

Suri said: (Nov 12, 2016)  
Correct @Anish.

Pratik said: (Apr 18, 2017)  
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.

Ayush said: (Jul 1, 2017)  
What about unboxing feature of wrapper classes which convert the wrapper class to primitive type?

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.