This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes a switch problem 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 "a switch problem" Watch "a switch problem" New topic

a switch problem

Gary Kevin
Ranch Hand

Joined: Jul 24, 2006
Posts: 43
why the result is 6?

public class Test
public static void main(String Args[]){
int i = 1,j = 0;
case 2:j+=6;
case 4:j+=1;
default: j+=2;
case 0: j+=4;

SCJP 5.0 72%
Gary Kevin
Ranch Hand

Joined: Jul 24, 2006
Posts: 43
I thought it should be 2.
Neelesh Bodas
Ranch Hand

Joined: Jul 20, 2006
Posts: 107
because there is no break after the default, and hence control falls through. thus j+=2 and j+=4 are both executed. This makes j=6.
[ July 24, 2006: Message edited by: Neelesh Bodas ]
Have you checked out Aspose?
subject: a switch problem
It's not a secret anymore!