Java Programming - Flow Control - Discussion

Discussion :: Flow Control - Finding the output (Q.No.6)

6. 

What will be the output of the program?

public class Switch2 
{
    final static short x = 2;
    public static int y = 0;
    public static void main(String [] args) 
    {
        for (int z=0; z < 3; z++) 
        {
            switch (z) 
            {
                case y: System.out.print("0 ");   /* Line 11 */
                case x-1: System.out.print("1 "); /* Line 12 */
                case x: System.out.print("2 ");   /* Line 13 */
            }
        }
    }
}

[A]. 0 1 2
[B]. 0 1 2 1 2 2
[C]. Compilation fails at line 11.
[D]. Compilation fails at line 12.

Answer: Option C

Explanation:

Case expressions must be constant expressions. Since x is marked final, lines 12 and 13 are legal; however y is not a final so the compiler will fail at line 11.


Mahesh said: (Jun 30, 2011)  
public class Switch2
{
final static short x = 2;
public static final int y = 0;
public static void main(String [] args)
{
for (int z=0; z < 3; z++)
{
switch (z)
{
case y: System.out.print("0 "); /* Line 11 */
case x-1: System.out.print("1 "); /* Line 12 */
case x: System.out.print("2 "); /* Line 13 */
}
}
}
}

// Can you tell me why the above program giving 012122 as output?

Mahesh said: (Jun 30, 2011)  
Nevermind i got the logic. I forgot when we define x and y as static final. They become constant. So inside switch case - case y will mean case 0 and so on for others.

Piya said: (Nov 11, 2011)  
When I define the keyword public for why it display the op as 012122 how is it?

Dev Singh said: (Jun 21, 2013)  
@Mahesh:

In your program output will be:

1st time (at z=0) condition (case y) : 012 (because No break).

2nd time (at z=1) condition (x-1) : 12 (again after 1 No break, so 2 printed).

3rd time (at z=2) condition (x=2) : 2.

So output : 012122.

Pallavi said: (Jun 3, 2014)  
Why is it a compilation error in line number 11 but not in 12 ?

Ramya said: (Dec 14, 2014)  
Why no error in line 13? I learnt type of case label should be same as 'x' (switch (x) ).

Maxwell said: (Sep 19, 2015)  
Why is y, (a public variable) not acceptable in a switch case? Thanks in Advance.

Raman said: (Sep 26, 2015)  
I declare why as final and not remove final so my program is success.

Its mean switch case acceptable public.

Neha said: (Sep 14, 2018)  
Here x is evaluated at compile time as x is a final variable but y is not a final variable so compiler evaluates y's value at runtime. So the value of is not considered as constant.

Partha said: (Oct 6, 2018)  
public class Main
{
final static short x = 2;
final static int y = 0;
public static void main(String [] args)
{
for (int z=0; z < 3; z++)
{
switch (z)
{
case y: System.out.print("0 "); /* Line 11 */
case x-1: System.out.print("1 "); /* Line 12 */
case x: System.out.print("2 "); /* Line 13 */
}
}
}
}

Here, if we change the static to final then the output : 012122.

Animesh said: (Jan 8, 2019)  
Why Y is not accepted in a switch case? Explain to me.

Juel Khan said: (Mar 22, 2020)  
In C/C++ int values are work well. But why in Java int values does not work? Please help anyone.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.