GeeCON Prague 2014*
The moose likes Java in General and the fly likes String in switch case Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "String in switch case" Watch "String in switch case" New topic
Author

String in switch case

Asvin Kumar
Greenhorn

Joined: Mar 01, 2010
Posts: 13
I want to use a string in switch case and I get the following error,

[javac] UserData.java:58: incompatible types
[javac] found : java.lang.String
[javac] required: int
[javac] switch(type)

any help??
Shinelin Samuel
Ranch Hand

Joined: Mar 01, 2010
Posts: 55
You cant use String in switch case, allows types are int and char.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

Actually, int and everything that is assignable to int. That includes char, short and byte. Since Java 5.0 enums can also be used in switch statements, and Java 7 may (finally?) introduce the switch statement for Strings. It has been discussed, but I don't know what the status on that is.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Asvin Kumar
Greenhorn

Joined: Mar 01, 2010
Posts: 13
Thanks for your replies...
 
GeeCON Prague 2014
 
subject: String in switch case