Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes can we use string in switch Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "can we use string in switch " Watch "can we use string in switch " New topic
Author

can we use string in switch

kalaiyarasan sivaprakasam
Greenhorn

Joined: Aug 17, 2009
Posts: 29
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

Joined: Mar 02, 2009
Posts: 151

yes we can


Start Earning Online||Start Earning Using Java
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18101
    
  39


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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36453
    
  15
You can use enum members, however.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

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

Joined: Feb 11, 2010
Posts: 27
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
internet detective
Marshal

Joined: May 26, 2003
Posts: 29219
    
135

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.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

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

I totally just said that.
satish varma
Greenhorn

Joined: Feb 11, 2010
Posts: 27
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
Sheriff

Joined: Sep 28, 2004
Posts: 18101
    
  39

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

Joined: Feb 11, 2010
Posts: 27
ohh really!! i dont know about that thing, ok i will search
Thank you
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2969
    
    9
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

Joined: Nov 06, 2006
Posts: 133
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

Joined: Mar 05, 2008
Posts: 2969
    
    9
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

Joined: Aug 17, 2009
Posts: 29
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

Joined: Oct 13, 2005
Posts: 36453
    
  15
The bit about "state variety" is incorrect syntax.
satish varma
Greenhorn

Joined: Feb 11, 2010
Posts: 27
yes me too agree with Ritchie
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: can we use string in switch
 
Similar Threads
switch on a string
switch with Strings
switch expression
switch case
Decoding Expresssions to values same as 'DECODE' function in Oracle