wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Jqplus Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Jqplus" Watch "Jqplus" New topic
Author

Jqplus

Vivek Shrivastava
Ranch Hand

Joined: Jun 03, 2000
Posts: 277
Hi,
This is from jqplus exam.
Q
Which of these combinations of switch expression types and case label value types are legal within a switch statement?

One of the answer is :
Switch expression of type char and case label of valur type byte

my doubt here is, what if case label have a -ve value because byte can have a -ve value.
am i making any sence here??

vivek
[This message has been edited by Vivek Shrivastava (edited August 12, 2000).]
Ken Lai
Ranch Hand

Joined: Aug 06, 2000
Posts: 30
Yeah I'm quite confused with the legal types in a switch statement. For example:
byte b = 10;
switch(b)
{
case 'a' :
break;
}
is legal but how is the conversion done? I mean if we assign a char to a byte the compiler would complaint but not in this case.
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Originally posted by Vivek Shrivastava:
Hi,
This is from jqplus exam.
Q
Which of these combinations of switch expression types and case label value types are legal within a switch statement?
[b]
One of the answer is :
Switch expression of type char and case label of valur type byte

my doubt here is, what if case label have a -ve value because byte can have a -ve value.
am i making any sence here??

vivek
[This message has been edited by Vivek Shrivastava (edited August 12, 2000).][/B]

U will get error as value too large.Since not compatible
with range of char.
sanjay gautam
Ranch Hand

Joined: Aug 13, 2000
Posts: 35
class Test{
public static void main(String[]s){
char='a';
byte=-10;
switch(b){
case 1:System.out.println(" i am in case one");
break;
case (-10):System.out.println(" i am in case byte");
break;
case 'a':System.out.println(" i am in case 'a'");
break;
}
}
}
try this it will work. thanks for a tricky question.
sanjay gautam
Ranch Hand

Joined: Aug 13, 2000
Posts: 35
please modify the solution provided by me earlier
byte b=-10; instead of byte=-10;
and char c='a';instead of char='a';
 
wood burning stoves
 
subject: Jqplus
 
Similar Threads
another small question
switch & case + for loop
switch statement
Switch statement
JQ+ swith question