• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

strange switch

 
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey java ppl
I am confused here Read this code
byte x=2;
switch(x)
{
case 'b': // 1
default : // 2
case -2: // 3
case 80: // 4
}
it compiles cleanly on the other hand
char c='o';
byte b=c; //gives error why ???
I thought that case label values should be assignable to switch argument but how can a char in first case 'b': get assigned to byte x

Regards Denish
 
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
a byte is small than a char (8 bits vs. 16 bits). For this code to work, you'll need an explicit cast:
b = (byte)c;
[This message has been edited by Lance Finney (edited May 08, 2001).]
 
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The reason your switch statement is ok is that switches can only take int arguments, so I believe that all values (x in the switch statement and the case labels) are promoted to int status.
However, if you tried to use more precise types (long, double, etc.) in for x or in any of the case labels, you would generate a compiler error due to loss of precision.
 
Ranch Hand
Posts: 216
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Denish,
My reaction to your specific querry is as follows:
The expression in the <code> switch(expression) { case lable: }</code> must be an integral type, i.e., it should be a data type that can
be promoted to an "int" viz., char, byte, short, int. The case
<code>lable </code> must be assignable to the type of integral statement. It cannot be boolean, long or floating point.
For more details on switch construct please visit the JLS section
14.10 at <A HREF="http://java.sun.com/docs/books/jls/second_edition/html/statements.doc.html#35518</a >" TARGET=_blank>http://java.sun.com/docs/books/jls/second_edition/html/statements.doc.html#35518 link.
In light of above, refer to your code, <code> switch(x)</code>. The expression "x" got promoted to "int" no
need to explicitely cast to int (implicite widening takes place).
However, in the second case

Hope this clarifies your querry.
Ravindra Mohan

[This message has been edited by Ravindra Mohan (edited May 08, 2001).]
 
denish mehta
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanx a lot guys.
Regards Denish
 
reply
    Bookmark Topic Watch Topic
  • New Topic