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 Default in 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 "Default in Switch" Watch "Default in Switch" New topic
Author

Default in Switch

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
I was expecting to see 'default', 100, 200 to be printed in this Class. But default does not print. Why??
New to the group and JAVA. Please comment:
public class SwitchTest {
public static void main(String args[]){
int test = 100;
switch(test){
case 20: System.out.println("20");
default: System.out.println("default");
case 100: System.out.println("100");
case 200: System.out.println("200"); //break;
}
}
}

Ajith Kallambella
Sheriff

Joined: Mar 17, 2000
Posts: 5782
Kesava,
Welcome aboard, bring in your friends too
The position of the default does not matter in the case construct. It can appear anywhere among the different case options. It will only get executed when no case statements match. In your case 100 matched the value 100.
Also note that because none of your cases have break after the println, the control will "fall through". If you put default after 100, you will see default too.
Ajith


Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
Ajay Kumar
Ranch Hand

Joined: Apr 28, 2000
Posts: 87
Hi,
If you put the case default: after the case 200: statement you will get the result you desire.
The way the switch statement works is once a matching case statement is found the execution starts at that statement and "falls through" the rest of the statements till a break is reached.
In you case it starts at case 100: and continues to case 200: before the conditional loop is exited.

public class SwitchTest {
public static void main(String args[]){
int test = 100;
switch(test){
case 20: System.out.println("20");
default: System.out.println("default");
case 100: System.out.println("100");
case 200: System.out.println("200"); //break;
}
}
}


Hope this helps.
------------------
Regds
Ajay Kumar
[This message has been edited by Ajay Kumar (edited June 02, 2000).]


Regds<BR>Ajay Kumar
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Ajit,
Now I understand the rule. JLS do not discuss the placement of 'default'.
I got hooked on to this site. I am planning to take SCJP test next week. I am doing OK on Java Ranch. I have looked into 'java traps' and other mock tests. Please fill me in on any other resources that you can think of.
Thanks a lot for reply..
Kesava
Ajith Kallambella
Sheriff

Joined: Mar 17, 2000
Posts: 5782
Did you look at www.javaranch.com/mock.html ??
Ajith
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Default in Switch
 
Similar Threads
try/catch??
Need Help building Calculator
Selection Statements
What is this?
default statement