• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

switch

 
jai raj
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have a pice of code

public class Q12
{
public static void main(String[] args)
{
char x = '1';

switch (x)
{
default:
System.out.println("default");
case 1:
System.out.println("case 1");
case 2:
System.out.println("case 2");
break;
}
}
}

The o/p is default case1 and case2
let me know
 
Srinivasa Raghavan
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because there is no break statement after each case.And a default block can come at place inside a switch statement. It's not necessarly be at last
And 'x' is a character, so there will be a implicit type cast to integer ( ascii value of 'x'), but you haven't provided it in any case statement. Hence the default is executed.
[ January 19, 2006: Message edited by: Srinivasa Raghavan ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic