Abhi
Abhi

Reputation: 2378

Java and switch case

Does any one know as to why Java does not allow you to switch on numbers larger than integers? Is there any technical reason behind this?

Upvotes: 9

Views: 8881

Answers (5)

Mario Fusco
Mario Fusco

Reputation: 13768

If you find the native java switch construct is too much limiting give a glance to the lambdaj Switcher that allows to declaratively switch on any object by matching them with some hamcrest matchers.

Upvotes: 0

kgiannakakis
kgiannakakis

Reputation: 104188

Scala, a language built on top of JVM, allows you to define your own case classes, that you can use in a switch statement. So, using longs or doubles or Strings in a switch statement is certainly possible.

I don't know however how complicated and how effective is it. With simple types, the compiler just calculates an offset to jump on a table. This is definitely not the case with more complex types.

I suppose the answer has do to with the time java was designed and the goals the designers were trying to achieve. Java initial goals were to design a "better C++", portable in many environments. I can understand why a switch on complex types didn't fit it in.

Upvotes: 1

mkoeller
mkoeller

Reputation: 4477

My guess is, that long was dropped as a valid switch expression type because of/related to the fact that operations on long variables may not be atomic.

Upvotes: 0

VonC
VonC

Reputation: 1326842

As said here, switch works with the byte, short, char, and int primitive data types. It also works with enumerated types and a few special classes that "wrap" certain primitive types: Character, Byte, Short, and Integer.

The Java switch statement is compiled into the JVM bytecode tableswitch or lookupswitch. Both of these bytecodes require that the case values be unique, 32-bit, integer, compile-time constants.

The tableswitch and lookupswitch instructions both include one default branch offset and a variable-length set of case value/branch offset pairs.
Both instructions pop the key (the value of the expression in the parentheses immediately following the switch keyword) from the stack.
The key is compared with all the case values:

  • If a match is found, the branch offset associated with the case value is taken.
  • If no match is found, the default branch offset is taken.

Even though the above represents implementation details, I believe the types used for switch are the one compatible with an efficient bytecode for control flow, and it may have been an important part for the reason of this choice.

As said in this Java Bug:

I suppose you could allow Java switch statement cases to include other types of values, and compile a switch statement that uses one of those kinds of values into other bytecode, such as a sequence of ifs and gotos. But then we'd have to examine all switch statements carefully in order to determine if it will be compiled into a fast switch bytecode or a slow sequence of if-elses.

As said in this other Java bug

'switch' statements can use function-table dispatch and/or binary-tree search to match the case statements faster than just testing them one by one in order.

Upvotes: 23

Gilles
Gilles

Reputation: 409

Maybe because it's really bad programming to have more than integer max cases in a switch?

If you want to switch over long variables, just projet them in the interger space.

Upvotes: -8

Related Questions