File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes switch Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "switch" Watch "switch" New topic


jai raj

Joined: Jan 18, 2006
Posts: 1
i have a pice of code

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

switch (x)
case 1:
System.out.println("case 1");
case 2:
System.out.println("case 2");

The o/p is default case1 and case2
let me know
Srinivasa Raghavan
Ranch Hand

Joined: Sep 28, 2004
Posts: 1228
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 ]

Thanks & regards, Srini
MCP, SCJP-1.4, NCFM (Financial Markets), Oracle 9i - SQL ( 1Z0-007 ), ITIL Certified
I agree. Here's the link:
subject: switch
It's not a secret anymore!