File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

switch

 
xie li
Ranch Hand
Posts: 54
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1 int i = 1,j = -1;
2 switch (i) {
3 case 0, 1:j = 1;
4 case 2: j = 2;
5 default; j = 0;
6 }
7 System.out.println("j="+j); What is the result?

A. j = -1
B. j = 0
C. j = 1
D. j = 2
E. Compilation fails.
 
harish shankarnarayan
Ranch Hand
Posts: 158
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
line 3,
case 0,1???


ans: Compilation fails
 
xie li
Ranch Hand
Posts: 54
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why?
 
vipul patel
Ranch Hand
Posts: 146
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Xie,

Answer is "Compilation fails!". Why? Note that java compiler does not understand everything you write.

1 int i = 1,j = -1;
2 switch (i) {
3 case 0, 1:j = 1;
4 case 2: j = 2;
5 default; j = 0;
6 }

Look at line 3. It is syntax error.
Look at line 5. It is syntax error as well in default; (instead of ;,

Correct Version of code is as follow.


1 int i = 1,j = -1;
2 switch (i) {
3 case 0: case 1:j = 1;
4 case 2: j = 2;
5 default: j = 0;
6 }
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic