aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes j++ 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 "j++ IN switch()" Watch "j++ IN switch()" New topic
Author

j++ IN switch()

Hades Pan
Ranch Hand

Joined: Nov 25, 2001
Posts: 106
class Foo{
public static void main(String args[]){
int x=4,j=0;
switch(x){
case 1:j++;
case 2:j++;
case 3:j++;
case 4:j++;
case 5:j++;
break;
default:j++;
}
System.out.println(j);
}
}
1. 1
2. Compiler error
3. 6
4. 3
5. 2
Ans is Compiler error but why?
Jim Hall
Ranch Hand

Joined: Nov 29, 2001
Posts: 162
Did you try to compile it? I see nothing wrong with the code and it compiles fine on my machine. The answer is wrong.
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9046
    
  10

I agree with Jim.


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Bunty Naidu
Greenhorn

Joined: Jan 10, 2001
Posts: 6
Ya Jim, you're right. The answer is 5)2.
Mr Iftikhar
Ranch Hand

Joined: Oct 12, 2001
Posts: 39
Yes the code is fine and will print 2 on the console.

java language
 
Don't get me started about those stupid light bulbs.
 
subject: j++ IN switch()