• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

switch case

 
Abhishek Mantri
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 201
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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;
 
Abhishek Mantri
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic