1

Sorry if you felt the post was negative. If you're here to learn something, I hope you learned something from it, such as a little taste of how a switch works and why this sort of thing simply can't work, considering what a switch actually is. To the compiler, it's not just shorthand for if()...else if()...eise if() even though that's what it looks like from this side.
So the theory behind the code is simply wrong, and if you'd tried it you would have found that it can't be made to work. That's why I said, please try your code before you post it if you're not sure. Actually, it'd be best to try it even if you are sure, but that's another thing.
Specifically, what this code tries to do, which can't be done, is to use a function call as the case of a switch.

I'm being very strong on this point because there are a lot of novice Java programmers reading this, and your post is likely to confuse some of them.

Does this mean you can never see a situation where this would be useful? How would you handle the OP's situation? How would you handle other situations that at first glance may look like a switch on strings would be useful?

The original poster didn't describe a situation, they just said they wanted "to use a switch statement on String". To me, this sounds a lot like "I want to hammer a nail with a flounder". Now, I can think of a few reasons why someone might think they needed to do that (String into switch, not nail/flounder). Mostly, those involve handling user input. But I can't think of any where the switch is the best solution. If you have a particular case where you think something like this is required, I'd be happy to figure out the best solution for that case, but I can't think of anything where the best solution involves torking a String until it fits into a switch.

Votes + Comments
I don't know what you have against using flounders as hammers.
0

Specifically, what this code tries to do, which can't be done, is to use a function call as the case of a switch.

Now I learned something! I was not sure this was the case but it does make sense. I am currently at work so I am unable to test anything right now or I would have given it a shot first.

The original poster didn't describe a situation

I just remembered he had said something about colors but I guess he didn't get specific with the situation.

0

Sorry, I thought I'd made that clear (about the function calls as cases) in the previous post. I guess I took it out, though. Anyway, what the switch generates looks more like a lookup table, where the if/else if/else if method generates a series of test-and-branch instructions. The lookup table is why you can't use dynamically generated code (although I think it'll allow wrapper classes of the primitives - but it'll actually throw an error, not an exception, if you screw that up, so better to use the primitives!)

I can look up some of my notes on this, but we only parsed the switch, we never compiled it in my compilers class, so I'm limited in the details I can give you.

-2

Switch to C#.C# switch statement accepts anything(int,bool,string...).If you cannot afford Visual Studio 2008 or 2010 Pro download free express edition.
Why the popular java language can accept only int with switch?
Hope it helps.

Talley
.NET Developer

Votes + Comments
No, actually this is quite useless.
0

Switch can accept int or char. Everything else would be better suited with an appropriate logic test inside of an if-statement.

...well it can take Strings too, if you parse them (switch(Integer.parseInt(string))) {}

But why bump a thread that is 3 months old?

Edited by ztini: n/a

0

Switch can accept int or char. Everything else would be better suited with an appropriate logic test inside of an if-statement.

...well it can take Strings too, if you parse them (switch(Integer.parseInt(string))) {}

But why bump a thread that is 3 months old?

Actually, for conversation's sake, that example wasn't very good because the String will not always be parsed to an int. What if the programmer wants this String "abc" to be put at the switch.

I believe that the switch should be used if you have a lot of options and you don't want to write multiple ifs. And since you cannot use switch you can use ifs with Strings very easily. I don't know the reason why the developers don't allow Strings in the switch but I hardly believe that this should be used as an argument to compare the 2 languages.

And I remember vaguely another member's comment about not being able to use Strings with switch.

"If you have a lot of if statements that compare Strings then the problem is not that you cannot use switch to make the code simpler, but your logic and your decision to write code with so many ifs".
And I am not talking about 3-5 ifs statements.

And personally, I never found myself in a situation that I wished I could use Strings in a switch.

0

Actually if Java 7 ever comes out, you can then use Strings in switches. You can, also, ever since they have existed, use Enums in switches.

-2

Huzzah, switch on a string.

I stand by my previous post, though: if you find you're switching on a String, you should take it as an alarm bell. It's almost certainly a sign that something has gone wrong with your design and you need to fix it.

0

Hi all, one of you guys answered the question in page 2, but everyone ignored it :P Here is an exemple of what he was saying. You can't technically switch on String, but this little method call just bypass the issue with (I think) some grace :D

Simple enum

public enum ClientType {
	poste, solution;
}

The switch

String clientType = "solution";
switch (ClientType.valueOf(clientType)) {
   case poste:
	break;
   case solution:
        // Will take this value
	break;
   default:
        break;
}

Can't be more object or simple.
BTW, it is case sensitive.

Edited by jeanrlavoie: n/a

0

Huzzah, switch on a string.

I stand by my previous post, though: if you find you're switching on a String, you should take it as an alarm bell. It's almost certainly a sign that something has gone wrong with your design and you need to fix it.

mmm. did you guys ever did a parameterisable application? Parameters are strings, and it becomes handy to let user write paremeters in full string then using "1-2-3-4-5-6-7-etc". When, you know, the user will need to know by heart what the parameter 4 stands for...
It's hard to avoid the main(String[] args) you know ;)

0

Hi all, one of you guys answered the question in page 2, but everyone ignored it :P Here is an exemple of what he was saying. You can't technically switch on String

you do know that Java 7 was released, and in Java 7 it ís possible to use a switch with a String?

0

I agree that java 1.7 will be released. It's on beta now. I would not do professional developpment over a beta... My own "rules".
Also, if you develop on multi platform, you'll have to consider java 7 won't be available on Apple platform for at least 2 years from today. (Btw, don't believe Apple when they say "We are fully java compatible". The new gear yes, but anything that was released before now may never be compatible... So, if you develop multi platform, you can still want to know how you can emulate java 7 ;)

Nice day

0

Actually, the new OS X does not come with Java installed, and Apple is, as far as I know, pushing Java development and maintenance off to the OpenJDK. Java on the mac is not a sure thing, not that it ever was very solid.

Honestly, I think Java 7 may be the last release of the Java language that anyone pays any attention to. I wouldn't be surprised if, five years from now, Java was a legacy language and everybody was just staying 1.6

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.