Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

...switch case...

Status
Not open for further replies.

patnim17

Programmer
Jun 19, 2005
111
US
I am trying to use the java Switch Case statement like this.
String Status;
switch (Status){
case "A":
break;
case "B":
break;
case "C":
}

but it does not take a String type in the case construct. java complains that it requires an int...

is there a workaround?
 
If you're using just the first character, use

Code:
String yourString;

switch (yourString.charAt(0)) {

}

Other workarounds are a bunch of ifs or, if you're using Java 1.5, define an enumeration:

java.sun.com/docs/books/tutorial/ java/nutsandbolts/switch.html

Cheers,
Dian
 
Use an if/else if/else block.

There is absolutely no point at all in attemping to use a switch statement on an object (as opposed to a primitive) because a switch statement is optimized for primtives where the stack size is known. With an Object, the size is not know till runtime - hence objects are stored on the heap, rather than on the stack like primitives. This means that from an optimized point of view, if a switch statement were to be able to cope with Objects, its optimized runtime would be ruined.

So ... use the if/else if/else construct.

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
I don't agree, the switch/case statement is more structured than a bunch of ifs. Including the switch/case with object would only involve modifying the compiler to transalte into a bunch of ifs at bytecodes.

Cheers,
Dian
 
Yes, but it isn't a bunch of if/else if/else statements - thats the point ...

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
It would be nice if the compiler could use the optimised stack approach for int-like selectors, whilst generating a non-stack version for selecting on objects. We'd get the best of both worlds then. I hate using if else if else if else ..........

Tim
---------------------------
"Your morbid fear of losing,
destroys the lives you're using." - Ozzy
 
I don't think it could be useful for general objects, as it's really diffcult to know an Object value at compile time.

I just claim it for Strings. It's really common to know at compile time which possible values an String will have.

That's not applicable, I think, to a general object. I'd never guess which value will have a HashMap or a URLEncoder instance.

Cheers,
Dian
 
I was kinda wondering if it would (theoretically) make use of the (possibly) overridden 'equals()' method.

Tim
---------------------------
"Your morbid fear of losing,
destroys the lives you're using." - Ozzy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top