• 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 use string in switch

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi all,

i have a doubt,

can we use string values inside switch in java?


can any one explain me with example..



with regards,
Kalai.
 
Ranch Hand
Posts: 151
MyEclipse IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes we can
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Not currently. Being able to use a string in a switch statement, is one of the proposed enhancements for Java 7 -- and will likely be adopted.

Henry
 
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can use enum members, however.
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

akhter wahab wrote:yes we can


In which version of Java are you running this? (Also, please make sure that if you post code to make it compilable, or indicate specifically that it won't. Thanks!)
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

yes we can




in wich version of java this code? as per my knowledge " byte,short,int,char" and their corresponding wrapper classes in addition to these "enum" are possible arguments
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As noted, above the String example is not valid. Currently (Java 6 and below), two values must be comparable with == to be used in switch. Since this is not the case for String, it cannot be used.
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

satish varma wrote:in wich version of java this code?


I totally just said that.
 
satish varma
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

two values must be comparable with == to be used in switch

check out that statements did you mean s1,s2 are not comparable?
ok any how, every case label must be a constant or a valid expression that gives constant as result. For that the possible switch arguments are type
byte,
short,
int,
char,
Byte,
Short,
Integer,
Character,
enum
here "enum" type value represents constant, and it avialable from JDK 1.5 version onwards. other than these types are invalid
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

satish varma wrote:

two values must be comparable with == to be used in switch

check out that statements did you mean s1,s2 are not comparable?



Since switch on string isn't supported yet -- a discussion about the implementation is kinda moot.

Regardless, with the proposed Java 7 implementation, I believe the proposal is for a switch on an integer (string hashcode), to be followed by a if check in the case body. And the check will be based on the value of the string (equals comparison).

Henry
 
satish varma
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ohh really!! i dont know about that thing, ok i will search
Thank you
 
Master Rancher
Posts: 4806
72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:Since switch on string isn't supported yet -- a discussion about the implementation is kinda moot.


Not entirely - JDK 7 milestone 5 has this feature. It hasn't been made it to a final production release yet, but an implementation is out there.
 
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ,

We can not use Strings inside the Switch statement. But we can use Compiletime Constants like final String TEMP in LABEL of the Switch statement.

Thanks for the link which explained proposed JAVA7 enhancement.

 
Mike Simmons
Master Rancher
Posts: 4806
72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Praveen Kumar wrote:We can not use Strings inside the Switch statement. But we can use Compiletime Constants like final String TEMP in LABEL of the Switch statement.


Ah, no.

In current Java releases (JDK 6), we cannot use Strings inside a switch statement, period.

In JDK 7, which is not yet officially released, we can use Strings inside a switch - both as the value being switched on, and the values in the case labels. For the case labels, the string values must be compile-time constants.
 
kalaiyarasan sivaprakasam
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

i am using java 1.5.

hi Ranch Hand,Hi satish varma


String state variety = condition ? "fish" : "fowl";

in this ,


i do not understand what is state ,what is variety?

as of i know both are string objects.


i do not think it will execute properly..

i know we can use enum

thanks for the link for java 7....


thank you very much to all



with Regards,
Kalai.


 
Campbell Ritchie
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The bit about "state variety" is incorrect syntax.
 
satish varma
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes me too agree with Ritchie
 
reply
    Bookmark Topic Watch Topic
  • New Topic