views:

698

answers:

9

Hi all, does Java have something similar to a branch or jump table? A branch or jump table table is, according to wikipedia, "a term used to describe an efficient method of transferring program control (branching) to another part of a program (or a different program that may have been dynamically loaded) using a table of branch instructions."

Does Java have something like this or do I just have to use if/else if/else or case statements?

Please let me know.

Thanks, jbu

+7  A: 

Java has a switch statement, but whether it compiles to a jump table in bytecode is implementation-dependent. In general, compilers will build you a jump table if they find nice constants for each case in your switch statement. I'm not sure you should really care how it's implemented, though. If you're coding in Java in the first place, you're probably just fine letting the compiler and the JIT take care of these things for you.

Note that switch only works with integer primitive types and enums, so you do need to use if/else statements if you're using other object types (and you probably shouldn't be comparing doubles or floats for equality anyway).

Finally, even though enum references are technically "constant", some compilers will only generate you a jump table when you switch on enums if your switch statement is in the same compilation unit where the enum is defined. Otherwise, it will generate you an if/else chain (as you'd have to do for regular objects). For the nitty gritty details, see the java.net forums on extending switch usage for Objects.

tgamblin
+1  A: 

I don't believe that you need those sort of performance hacks in Java. I would concentrate first of writing readable code and using decent algorithms - these will deliver more performance benefits than what you're discussing.

In most standalone applications, the vast majority of time is spent sitting around waiting for the user to do something. In most web applications, the amount of time running bytecode in the JVM should be swamped by network time, database time or business logic.

If you're really worried about the performance of part of your Java app, you can move it into JNI code and bypass the Java interpreter altogether.

paxdiablo
A: 

I think that is how some switch statements are implemented "under the hood" so to speak.

Other than that, you could do something similar with something like HashMap<whatever, Method>, where you use map.get(something).invoke(). But that kinda defeats the purpose because it wouldn't be as fast as a jump table, and I can't think of a good case where OOP programming/polymorphism wouldn't do the job better and cleaner.

Kip
A: 

You could do this with reflection and a generic HashMap that stored anonymous inner classes but it would be a horrible hack.

It is really elegant to do in C# due to native anonymous methods, but not in java.

FlySwat
A: 

THe kind of branch/jump tables you're talking about are not directly provided by high level languages such as Java, C, etc., but are generated by compilers in machine code or byte code. In other words, your compiler may use them but you don't see them.

jdigital
+2  A: 
OscarRyz
+1 to this and keysersoze, /-1 to EVERYONE ELSE for not realizing that OOP IS an implementation of a dispatch table! It's like asking why doesn't C have a push command to directly manipulate the stack.
Bill K
+1: Unless there's some _very_ unusual constraint, or the solution is one-off and very informal/small, Virtual dispatch should be the preferred dispatch mechanism.
Greg D
Another great Google Techtalk to see the replacement of conditionals with polymorphism (which is a refactoring pattern in Martin Fowler's book - 'Refactoring: Improving the Design of Existing Code') can be found here: http://www.youtube.com/watch?v=4F72VULWFvc
Dinuk
+2  A: 

a term used to describe an efficient method of transferring program control (branching) to another part of a program (or a different program that may have been dynamically loaded) using a table of branch instructions."

One way to transfer program control is to call a function. One way to call the right function when you have several to choose from, is to key it off of the type of an object. That's called polymorphism. Java and other Object Oriented languages have polymorphism, implemented through inheritance (subclassing). Not sure how it's implemented in Java, but in C++ there's (generally? or always?) a pointer in each object that points to the v-table for it's class, which contains pointers to virtual functions.

I seriously doubt the lack of a user-defined jump table is going to cripple the performance of your java application.

KeyserSoze
Perfect answer. Wish I could +3
Bill K
A: 

You can use enum to do this.

// doesn't work in c#
enum Switch implements Runnable {
   OPTION1() {
     public void run() {
        // do something.
     }
   },
   OPTION2() {
     public void run() {
        // do something.
     }
   },
   OPTION3() {
     public void run() {
        // do something.
     }
   }
}

Switch option = Switch.valueOf(switchOptionTest);
option .run();
//or
Switch[] options = Switch.values();
Switch option = options[nSwitchOption];
option .run();
Peter Lawrey
A: 

Yes, absolutely.

If you code a switch statement, then depending on various things the switch is converted into a tableswitch instruction in bytecode. Generally

  • The switch must depend on an int value
  • The highest int value and the lowest int value must not be too far apart

The simplest way to acheive this is to use java enums, which are handled specially by the compiler. The relevant documentation is in the Java Virtual Machine Specification. Of course, the JIT compiler almost certainly converts these directly into very fast switches in the machine code of whatever platform you are running on.

Having said that, the real answer to your question is "this is the sort of thing that you worry about when you are doing machine code, not programming in a high-level language".

paulmurray