Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can switch() be given more capability ?

 
Rahul Sudip Bose
Ranch Hand
Posts: 637
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It can only handle ints, chars...and enums. Why cant they make it take strings as well ?

 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java 7 has an update for that- Project Coin

[Edit: Added the Links]
 
Henry Wong
author
Marshal
Pie
Posts: 20902
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rahul Sudip Bose wrote:It can only handle ints, chars...and enums. Why cant they make it take strings as well ?


I believe this is an enhancement for Java 7.

[EDIT: beaten to the answer again]

Henry
 
Rahul Sudip Bose
Ranch Hand
Posts: 637
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just started making my own class PowerSwitch{} for fun- please tell me if i am thinking in the right direction. (its just in the planning phase)
this will take strings, convert them to lowercase and use them as keys in a map. these keys will be mapped to int objects which will be used in the switch of this class.

Any suggestions , ideas etc welcome.

PS : i was wondering if i should leave this since others are already doing it and...well there is scjp
 
Rahul Sudip Bose
Ranch Hand
Posts: 637
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
Rahul Sudip Bose wrote:
[EDIT: beaten to the answer again]


i am not so sure what this means.
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can always map the int with some string values, use the int for switch and then retrieve the string based on the int value.
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rahul Sudip Bose wrote:
Henry Wong wrote:
[EDIT: beaten to the answer again]


i am not so sure what this means.


that means- there a difference of less than a minute between my and Henry's answer.
 
Campbell Ritchie
Sheriff
Posts: 48441
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rahul Sudip Bose wrote:It can only handle ints, chars...and enums. Why cant they make it take strings as well ?

. . . or anything which can be implicitly cast to an int, ie shorts and bytes.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic