• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

final - switch

 
Robbie kyodo
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

static final y = 3 ;
switch (3) {
case y - 1: Do something }

Is this legal , taken from MasterExam y is a final and in switch it try to modify it.
The ans says it is legal
 
Yi Meng
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Robbie kyodo:
Hi

static final y = 3 ;
switch (3) {
case y - 1: Do something }

Is this legal , taken from MasterExam y is a final and in switch it try to modify it.
The ans says it is legal

I can't find anywhere in the program that tries to modify y.
y-1 is just an expression, it evaluates to 2 in this case at complie time since y is final and the value of y is still 3.
 
Yi Meng
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
However, i suppose you may probably have seen sth that reads "case value must be a constant", you may doubt why y-1 is legal here as the case value.
The trick here is all because y is final, and at compile time all occurrence of y is replaced with the value 3 which makes the expression y-1 to be 3-1, aka constant 2.
you may want to have a look at JLS $15.28
 
Robbie kyodo
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Yimeng
So y-1 only evaluated during run-time at that point. after that line y is still 3.
 
Yi Meng
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So y-1 only evaluated during run-time at that point. after that line y is still 3

y-1 is evaluated in compile time .....
all occurrences of y is replaced with the value 3 at compile time and there is actually no y at runtime.....
 
Barkat Mardhani
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One quick question:
Is it not true that switch statement should have integer variable in paranthesis:
switch(integer variable)
If so how is :
switch(3)
acceptable?
Thanks
Barkat
 
Yi Meng
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you shall try out ......acceptable.....
 
Anupam Sinha
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
switch(3) is fine because you can use a variable or a literal as well.
 
Sam Walker
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I'm a little stuck in the semantics of this switch statement, could someone please help me with that? Here's what I'm confused about, why would anyone want to examine the value of a constant? I always thought of a switch statement as a shorter way of writing if (exp) else if (exp) .. else. In that case I'd expect the switch() would resolve to a valid conditional statement that makes sense. But is if ( 3 ) else if (2) ... valid conditional statements?
 
Anupam Sinha
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sam
Well you are correct that switch is infact a shorter way for if...else. It makes sense to use switch statement in case you need to check a few if cases. But there is a limitation, a switch can only replace an if statement if that if uses an integral value to determine the result of the if condition. That is if you use two string comparison or say compare two double then you can not use switch.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic