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

switch case

Abhishek Mantri
Greenhorn

Joined: Sep 06, 2007
Posts: 25
i have following two doubts from K & B book:
1.
final int a = 1;
final int b;
b = 2;
int x = 0;
switch (x) {
case a: // ok
case b: // why this is compiler error


2.
byte g = 2;
switch(g) {
case 23:
case 128:
}
in the preceding code byte is cast to an int then why 128 is large as case constant?
manny singh
Greenhorn

Joined: Jan 18, 2008
Posts: 28
1) final int b=2; (must be initialized as declared else only allowed in construtors)

2) 128 = -1 ?

Right or not?
Sandeep Bhandari
Ranch Hand

Joined: Apr 16, 2004
Posts: 201
Originally posted by Abhishek Mantri:
i have following two doubts from K & B book:
case 128:
in the preceding code byte is cast to an int then why 128 is large as case constant?


can you compile this:

byte b = 128;


SCJP 96% | SCWCD 90%| SCJP mock exams | My SCJP blog
Abhishek Mantri
Greenhorn

Joined: Sep 06, 2007
Posts: 25
Thanks guys
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: switch case