File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Problems understanding the Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Problems understanding the "default case" in a switch statement" Watch "Problems understanding the "default case" in a switch statement" New topic
Author

Problems understanding the "default case" in a switch statement

Kai Wähner
Ranch Hand

Joined: Sep 16, 2009
Posts: 76
Hi there,

I thought that the default case matches every expression, isn't this correct?

But on the SCJP mock exam, a question occured something like this:



I know that it is not allowed to use two expressions with case 3 in a single switch statement, but I thought "default" will match the "3" expression (because it matches everything) and print "Invalid month.", but the solution says that case 3 "March" is correct?
Can you please explain this? Thank you.


Best regards,
Kai


My IT-blog about my experiences with: Java / JEE, IT Conferences and IT Certifications

Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9280
    
  17

Even though your default is in the middle of other case statements, this doesn't mean the case labels after the default will be ignored. Since the switch expression is 3, so the case labeled 3 will be a match and thus the default case won't be used...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4240
    
    7

default doesn't match everything, it matches anything not explicitly matched by a case expression. So in your case case 3 takes precedence.
ujjawal rohra
Ranch Hand

Joined: Mar 20, 2010
Posts: 103
Yes default matches everything BUT ONLY when there is no exact match available.
In your example if there was no case 3: then default would have surely matched.

Hope this would help


SCJP 6
Kai Wähner
Ranch Hand

Joined: Sep 16, 2009
Posts: 76
Thank you for your comments. Now it is clear.

I thought that each case is evaluated one after another, but all are evaluated at once (otherwise the default would not know that there will be another case that matches...)
ujjawal rohra
Ranch Hand

Joined: Mar 20, 2010
Posts: 103
You are welcome dear
Sandra Bachan
Ranch Hand

Joined: Feb 18, 2010
Posts: 434
The position of default is irrelevant, it can be in the beginning of the case statement


Marriage Made in Heaven
http://www.youtube.com/user/RohitWaliaWedsSonia
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problems understanding the "default case" in a switch statement
 
Similar Threads
Switch statement
Wrong answer in the JavaRanch Rule Round-up Game
switch
i want to get the month of a particular date
cannot find symbol inside String?