• 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

switch

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can someone clarify the rules for switching on a variable smaller than an int.
I know that we can only switch on byte, short, int or char. But what if we switch on say a byte:
byte byt = 17;
switch (byt) {
case 17:
System.out.println("17");
break;
case 120:
System.out.println("120");
break;
case 250:
System.out.println("250");
break;
}
The case 250 gives a loss of precission compile time error.
I thought java might promote the byte to an int.
and then then test like so:
if (byt == 250) System.out.println("250");
Which is legal. Could any of you ranchers clarify the actual rules for switch statements.
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Kelly,
You're attempting a narrowing conversion hence the error.
You get a loss of precision error when you attemt to put a larger primitive (in your case an 250 is outside the range for a byte) into a smaller primitive. You need an explicit cast to to tell the compiler you're aware of the danger to get round the problem.
See below:

public class NarrowConversion {
public static void main ( String [] args ) {
byte byt = 17;
switch ((int)byt) {
case 17:
System.out.println("17");
break;
case 120:
System.out.println("120");
break;
case 250:
System.out.println("250");
break;
}
}
}

Hope this helps.
 
Ranch Hand
Posts: 522
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Some switch statement rules:
  • The switch statement accepts Expression as a parameter. This expression must be a char, byte, short or int, or a compile time error will occur.
  • Every case constant expression associated with a switch statement must be assignable to the type of the switch Expression.
  • No two of the case constant expressions associated with a switch statement may have the same value.
  • At most one default label may be associated with the same switch statement.
  • Switch label value must always be compile-time constant.


  • See JLS � 14.10 for more informations.
    [ February 02, 2004: Message edited by: Vicken Karaoghlanian ]
     
    Put the moon back where you found it! We need it for tides and poetry and stuff. Like this tiny ad:
    a bit of art, as a gift, the permaculture playing cards
    https://gardener-gift.com
    reply
      Bookmark Topic Watch Topic
    • New Topic