This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes argh! why can't  a switch selector be a string! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "argh! why can Watch "argh! why can New topic
Author

argh! why can't a switch selector be a string!

Eric Johnson
Ranch Hand

Joined: Apr 30, 2001
Posts: 49
gotta vent. it would make life so much easier if i could use a string to determine which case to call. i'm sure there is a good reason why ya can't (which is probably beyond me), but what is that reason?
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9044
    
  10
You can use a char.

As to why, you'll probably have to ask James Gosling and his co-founders why they only allowed primitive numerics rather than Strings (or other Objects). I could guess, but ...


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
C# allows Strings.


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055
What's wrong with if-thens?
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
If you look at the JVM Spec, there's an instruction called tableswitch which is how switch statements typically get implemented. It's a fast way of getting to a particular spot in your code based on the switch value, without doing a lot of comparisons. (It also takes more memory than multiple comparisons would, but that's usually considered a fair trade.) This mechanism works pretty well as long as you want to switch on some sort of integer value - at least, one in a reasonable range. Longs are not allowed in switch statements, because they would require too much memory to implement simply with tableswitch. Strings and other objects would be more complex, much less amenable to treating with tableswitch. I suppose you could use a tableswitch as part of a hashtable implementation which would look up the correct jump point. But Gosling and company chose to keep the JVM and compiler requirements relatively simple in this area. Restricting the types of inputs allowed in switch() means you end up using tableswitch for those situations it works well in, and you're forced to find alternatives where it wouldn't work as well.
I do think a switch statement looks better than a long series of if / else if statements, and it would be nice to have this option. I wonder how it's implemented in C# for objects? Something hashtable-based I'm guessing. I'll have to take a look at that. But generally, using if / else if works fairly well most of the time, and there are often other OO-type alternatives if this is unsatisfactory.


"I'm not back." - Bill Harding, Twister
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Jim Yingst:
there are often other OO-type alternatives if this is unsatisfactory.

For example a Map mapping String's to instances of a Strategy.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
rom chatterjee
Ranch Hand

Joined: Dec 11, 2001
Posts: 46
If the strings you want to switch on are constant (eg Days of the week). Then define a set of constants with meaningful names and switch on those: MyConstant.VALUE


Rom Chatterjee<BR>Sun Certified Java Programmer
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by rom chatterjee:
If the strings you want to switch on are constant (eg Days of the week). Then define a set of constants with meaningful names and switch on those: MyConstant.VALUE

Are you suggesting to switch on String constants? That doesn't work.
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
No - he is saying that you can assign integer value to represent the constant Strings and switch on the integers.


"JavaRanch, where the deer and the Certified play" - David O'Meara
Mr. C Lamont Gilbert
Ranch Hand

Joined: Oct 05, 2001
Posts: 1170

A chain of if statements is no more difficult to code than a switch statement anyway. Whats the beef?
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
It's the aesthetics of the code .
Steve Deadsea
Ranch Hand

Joined: Dec 03, 2001
Posts: 125
http://developer.java.sun.com/developer/bugParade/bugs/4269827.html
for why case statements can be better than ifs and why it is hard to do optimised ifs sometimes.
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055

It's the aesthetics of the code .

Now we're getting into a pretty sensitive topic .
Personally, I find

more aesthetically pleasing than

but try convincing a programmer who thinks differently .
Ta Ri Ki Sun
Ranch Hand

Joined: Mar 26, 2002
Posts: 442
ah but you dont have to scroll back and forth on a 17" monitor to see the code, if's are pain when nested too deep unnecessarily
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Taariq Levack:
ah but you dont have to scroll back and forth on a 17" monitor to see the code, if's are pain when nested too deep unnecessarily

But most often this is a design issue, not a syntax or formatting issue...
 
 
subject: argh! why can't a switch selector be a string!
 
Similar Threads
indexOf implementation in Java
How does this output comes?
Combining Home and Remote interfaces in EJB
Putting digits one by one
a question about a String method...