aspose file tools*
The moose likes Beginning Java and the fly likes String support for switch case statement Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "String support for switch case statement" Watch "String support for switch case statement" New topic
Author

String support for switch case statement

swapnel surade
Ranch Hand

Joined: Mar 05, 2009
Posts: 129
Hi,

why java doesn't support String in switch case statement.

thanks
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11494
    
  16


Why should it support Strings in a switch case?


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18985
    
    8

It will in Java 7, apparently. I don't know why, but for some reason a lot of people wanted that feature.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40027
    
  28
Some other languages do. Eiffel claimed to support strings in its equivalent of a case about 20 years ago.
swapnel surade
Ranch Hand

Joined: Mar 05, 2009
Posts: 129
its very general requirement. suppose if you are using actionperformed() for button in swing. in that method you will get actionCommand() which returns you String. If there is switch statement then you can perform the operation easily, rather than writing if else.
swapnel surade
Ranch Hand

Joined: Mar 05, 2009
Posts: 129
I want to know Is there any concept given by SUN for not providing switch case for string.
D. Ogranos
Ranch Hand

Joined: Feb 02, 2009
Posts: 214
fred rosenberger wrote:
Why should it support Strings in a switch case?


I think many people see Strings as a primitive type (although they "know" that Strings are objects). Strings have special rules already that allow us to use them like primitives, for example in assignments or expressions. And Strings are immutable "constants", so it would feel natural to use them as case constants.

I think the reason why they can't be used so far is that the JVM switch instructions do not support object types.
swapnel surade
Ranch Hand

Joined: Mar 05, 2009
Posts: 129
but enum is supported by java in switch statement.
They are also objects...
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19781
    
  20

But each enum object has an order() that is a primitive and with which the object can be identified; it is unique within the same enum class. Strings do not have a similar property.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Lee Kian Giap
Ranch Hand

Joined: Jan 23, 2008
Posts: 213
because two different String object can have same value , should you consider it as same or different in the "case" ?


SCJP 6, SCWCD 5, SCBCD 5
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1836
    
    7

swapnel surade wrote:its very general requirement. suppose if you are using actionperformed() for button in swing. in that method you will get actionCommand() which returns you String. If there is switch statement then you can perform the operation easily, rather than writing if else.


You could easily work around that using an enum, by making use of Enum's toString() and valueOf() implementations.


Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40027
    
  28
swapnel surade wrote:its very general requirement. suppose if you are using actionperformed() for button in swing. in that method you will get actionCommand() which returns you String. If there is switch statement then you can perform the operation easily, rather than writing if else.
Yes, you could, but I don't like the design at all. It breaks the link between the button and the action, so it makes maintenance difficult.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String support for switch case statement