This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "switch" Watch "switch" New topic
Author

switch

jai raj
Greenhorn

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)
{
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

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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: switch
 
Similar Threads
return in Switch case
break using label
Switch statement
Default value in switch construct
switch statement