Java Programming - Flow Control - Discussion

Discussion Forum : 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 */
            }
        }
    }
}
0 1 2
0 1 2 1 2 2
Compilation fails at line 11.
Compilation fails at line 12.
Answer: Option
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.

Discussion:
12 comments Page 1 of 2.

Mahesh said:   1 decade ago
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?

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

Dev Singh said:   1 decade ago
@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.

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

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

Raman said:   1 decade ago
I declare why as final and not remove final so my program is success.

Its mean switch case acceptable public.

Juel Khan said:   6 years ago
In C/C++ int values are work well. But why in Java int values does not work? Please help anyone.

Ramya said:   1 decade ago
Why no error in line 13? I learnt type of case label should be same as 'x' (switch (x) ).

Maxwell said:   1 decade ago
Why is y, (a public variable) not acceptable in a switch case? Thanks in Advance.

Piya said:   1 decade ago
When I define the keyword public for why it display the op as 012122 how is it?
(1)


Post your comments here:

Your comments will be displayed after verification.