• Post Reply Bookmark Topic Watch Topic
  • New Topic

Questions about switch  RSS feed

 
Stu Thompson
Hooplehead
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
While tinkering around with switch (to better understand it) I have three silly questions that I cannot resolve.

1) why does switch not work with long expressions and lables? yea, i know longs are big numbers and all...but seems like an artificial limitation.

2) i can compile with a switch statement with a lable after the default label. i can't see any valid reason to do this, though, and kind of think it is the kind of thing that should result in a compile time error. or am i missing something?

3) why is this leagal? "switch (2){}" what happens behind the scenes?

thanks for your deep thoughts!

Stu



[ June 20, 2006: Message edited by: Stu Thompson ]
[ June 20, 2006: Message edited by: Stu Thompson ]
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. cases are limited to int for efficiency in building jump tables - there is no rational reason to use longs which would require 64 bits and two memory accesses to load the value.
2. flexibility - since the default can "fall through" to a numbered case
3. The definition of switch says it has to evaluate the expression to an int. What is your problem with this?

Bill
 
Stu Thompson
Hooplehead
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. OK. Have just learned what a jump table is and why this is so.
2. Humph. I thought I read something about the compiler requiring accessibility (?) and my example switch structure seems to have an easily detectable chunk of dead code.
3. Let me clarify: I am ok with 2 as an expression; it is the empty switch block that perplexes me.

Thanks for the switch statement demystifying answers.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Stu Thompson:

2. Humph. I thought I read something about the compiler requiring accessibility (?) and my example switch structure seems to have an easily detectable chunk of dead code.


It's not dead code. Switch doesn't work by looking at each case in turn; it works by branching directly to the matching case. If switch cases were boolean expressions, like an "if-else" chain, then they'd need to be in order; but that's not how switch works.

By convention, the default case is generally placed last. But you can put it first if you want -- everything will still work.
 
Stu Thompson
Hooplehead
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
2. Ohhhh....OK. That makes sense. I had it in my head that switch statements compiled like if-else chains. (Must be an artifact from learning whatever language way back when.)

Thanks.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Depending on the case arguments, in some cases they can compile to a short series of if / else statements. But jump tables are typical, unless the number of options is small. Regardless, even if an if / else structure is used, the compiler is smart enough to generate something that will work correctly even if you put the default somewhere other than the end.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jim Yingst:
Depending on the case arguments, in some cases they can compile to a short series of if / else statements.


If I recall correctly, there are two different bytecodes that can implement switch. I believe one is called "tableswitch" and one is called "lookupswitch". One is a pure jump table, where the switch value is used an an index into the table; the other is a linear search table lookup, which is the case that Jim is referring to. But I didn't think they were ever compiled into an actual chain of test and jumps -- are they?
 
Stu Thompson
Hooplehead
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys for getting into more detail on how switch is implemented. Understanding how things works behind the curtain is important to me.

Originally posted by William Brogden:
1. cases are limited to int for efficiency in building jump tables - there is no rational reason to use longs which would require 64 bits and two memory accesses to load the value.


Mildly off topic: If we were working with a 64-bit JVM/JDK, then this could potentially be not an issue, yes? Or is the 32-bit nature of switch expressions part of the language and expected to remain the same across all platforms?

Another question: Are <32-bit data type expressions (byte, short) implicitly cast/implemented as 32-bit value in the jump table? This seems instinctively safe to me and OK, but�
[ June 20, 2006: Message edited by: Stu Thompson ]
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You know EFH, I have a vague memory of once finding a particular compiler and set of circumstances in which a switch came out the same as an if / else if. It doesn't seem to be the case for recent Sun compilers however, and it's also quite possible I'm just remembering a lookupswitch, which as you note can be much like a series of if / else statements. I seem to remember there was a third way I found in which switch was sometimes implemented, but it didn't show up in the quick tests I ran just now. I may well be misremembering.

I would note that a lookupswitch isn't necessarily a linear search. The instruction requires that the match-offset pairs be sorted in increasing numerical order by match, in order to support faster lookups than linear, e.g. a binary search. I did some performance tests which suggested the search time is indeed faster than linear on my machine, but it's hard to measure precisely.

[Stu]: Or is the 32-bit nature of switch expressions part of the language and expected to remain the same across all platforms?

It's part of the language.

[Stu]: Are <32-bit data type expressions (byte, short) implicitly cast/implemented as 32-bit value in the jump table?

Yes, for both lookupswitch and tableswitch. You can find details in the JVM Spec, in the instruction set and under 7.10 Compiling Switches.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!