Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

can we use string in switch

 
kalaiyarasan sivaprakasam
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • 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.
 
akhter wahab
Ranch Hand
Posts: 151
Java MyEclipse IDE Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes we can
 
Henry Wong
author
Marshal
Pie
Posts: 20997
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use enum members, however.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • 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!)
 
satish varma
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • 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
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34178
340
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • 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
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • 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
  • 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
Marshal
Pie
Posts: 20997
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
ohh really!! i dont know about that thing, ok i will search
Thank you
 
Mike Simmons
Ranch Hand
Posts: 3037
10
  • Mark post as helpful
  • send pies
  • 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.
 
Praveen Kumar
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • 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
Ranch Hand
Posts: 3037
10
  • Mark post as helpful
  • send pies
  • 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
  • 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
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The bit about "state variety" is incorrect syntax.
 
satish varma
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes me too agree with Ritchie
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic