• 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

can we pass floating point values in switch

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi there,
can we pass floating point values in case labels in case of switch.though i know the answer..just want to know does my answer match yours.well yes we can pass floating point to case labels in Switch.

Thnx
 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well rajeev , that is true in C or C++ but not allowed in Java. Try and see it !
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
only byte, short, int and char type values can be used in case label.
 
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, you may not.
Zak Nixon
SCJP 1.4
 
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also case arguments can not be bigger than 'before promoted (to int) type of switch argment. For example:
byte b = 3
switch(b) {
case: 128 // not allowed bigger than byte.
}
 
Author
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the larger issue here is that it is the mark of a good computer programmer that he or she exhausts efforts to answer his or her own question before taking it to others. That case constants compile time constants of type int is a simple fact checking exercise.
 
Doug Dunn
Author
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay before I am justly flamed, the type of the expression in a switch statement must be char, byte, short, or int, and case constants must be compile-time constant expressions that are assignable to the type of the expression.
 
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Vinod -

well rajeev , that is true in C or C++ but not allowed in Java. Try and see it !


Not true! From Visual C++ Help:

The type of switch expression and case constant-expression must be integral. The value of each case constant-expression must be unique within the statement body.


If you have a compiler that's letting you have a non-integer switch argument, it's most likely one to avoid. Because the switch only tests for equality, it wouldn't even make sense to have a float-type argument.
 
Those are the largest trousers in the world! Especially when next to this ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic