jQuery in Action, 2nd edition*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes final - switch Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "final - switch" Watch "final - switch" New topic
Author

final - switch

Robbie kyodo
Ranch Hand

Joined: May 05, 2003
Posts: 97
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


SCJP 2 1.4
Yi Meng
Ranch Hand

Joined: May 07, 2003
Posts: 270
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.


Meng Yi
Yi Meng
Ranch Hand

Joined: May 07, 2003
Posts: 270
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

Joined: May 05, 2003
Posts: 97
thanks Yimeng
So y-1 only evaluated during run-time at that point. after that line y is still 3.
Yi Meng
Ranch Hand

Joined: May 07, 2003
Posts: 270
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

Joined: Aug 05, 2002
Posts: 787
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

Joined: May 07, 2003
Posts: 270
you shall try out ......acceptable.....
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
Hi
switch(3) is fine because you can use a variable or a literal as well.
Sam Walker
Ranch Hand

Joined: Nov 06, 2002
Posts: 65
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

Joined: Apr 13, 2003
Posts: 1088
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: final - switch
 
Similar Threads
AutoBoxing - Compilation Error
Switch And Primitive Wrappers
Question about local variables in switch
Passed Mock Exam with 75% --> Some questions about wrong answers...
Switch Defination.