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

String support for switch case statement

 
swapnel surade
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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

thanks
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12097
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Why should it support Strings in a switch case?
 
Paul Clapham
Sheriff
Pie
Posts: 20955
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Some other languages do. Eiffel claimed to support strings in its equivalent of a case about 20 years ago.
 
swapnel surade
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to know Is there any concept given by SUN for not providing switch case for string.
 
D. Ogranos
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but enum is supported by java in switch statement.
They are also objects...
 
Rob Spoor
Sheriff
Pie
Posts: 20511
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Lee Kian Giap
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
because two different String object can have same value , should you consider it as same or different in the "case" ?
 
Jelle Klap
Bartender
Posts: 1951
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic