jQuery in Action, 2nd edition*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes somebody please explain me this 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 "somebody please explain me this" Watch "somebody please explain me this" New topic
Author

somebody please explain me this

leena rane
Ranch Hand

Joined: Aug 13, 2001
Posts: 280
hi,
In round-up i saw this question:
In switch stmt,argument to the case label(case:argument) can be any variable which can fit within an int.
I said true,
i got it wrong.
The answer says:
false
the case argument must be either an int literal or an int -compatible variable which is a constant(i.e. static final)
Thanx in advance,
Best Regards,
leena.
Cherry Sta. Romana
Greenhorn

Joined: Aug 08, 2001
Posts: 18
Hi leena,
I think you were just confused about the question. The question refers to the argument following the case construct not the switch.
In the switch construct --> switch (expression) --> expression can be anything that is compatible to an int.
On the other hand, for the case construct, it has to be an int constant.
For example:
int a = 5;
int b = 10;
switch (a*2) // this is possible, value is evaluated
{
case 10: // should be a constant int
case b: // this is not possible since b is a var
}
I hope this helps.


Guy Allard
Ranch Hand

Joined: Nov 24, 2000
Posts: 776
Also note that:
final int x = 20; // OK to use 'x' in 'case' because of 'final'
Regards, Guy
Gagan Indus
Ranch Hand

Joined: Feb 28, 2001
Posts: 346
Nice explanation Cheery and
Very good point Guy Allard
please also note tht expressions involving final variables (along with literals only), which results in value assignable to the type of swicth-expression , can also be used as case labels

output is :
Good Luck for ur exam!

even float , double final-variables can be used in the expression , but the final result has to be cast into int's ( byte , char , short , int )

still , output is :
Good Luck for ur exam!

------------------
Gagan (/^_^\)


Gagan (/^_^\) SCJP2 SCWCD IBM486 <br />Die-hard JavaMonk -- little Java a day, keeps you going.<br /><a href="http://www.objectfirst.com/blog" target="_blank" rel="nofollow">My Blog</a>
leena rane
Ranch Hand

Joined: Aug 13, 2001
Posts: 280
Thanx Cherry,Guy and Gagan 4 such nice explanation .
Now the point is clear to me!!
Best Regards,
leena
Ashik Uzzaman
Ranch Hand

Joined: Jul 05, 2001
Posts: 2370

Good explanation Gagan!
------------------
azaman


Ashik Uzzaman
Senior Member of Technical Staff, Salesforce.com, San Francisco, CA, USA.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: somebody please explain me this
 
Similar Threads
//question of round-up//
JavaRanch #223
java ranch #223 and #325
java round-up game #223
switch