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.

Ayush said:   8 years ago
What about unboxing feature of wrapper classes which convert the wrapper class to primitive type?

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.

Suri said:   9 years ago
Correct @Anish.

Anish kumar said:   9 years ago
From 1.5 version all wrapper class is allowed e.g Integer, Short.
From 1.7 version string is also allowed.

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)

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.

AJAY said:   1 decade ago
What is the meaning of 1.4v?

Thanks in advance.

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.

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.

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.


Post your comments here:

Your comments will be displayed after verification.