Is there something like a branch / branch table in Java?

Does Java have something similar to a transition or transition table?

Table of branches or transition tables, according to wikipedia ,

A term used to describe an effective method of transferring program control (branching) to another part of a program (or another program that can be dynamically loaded) using a jump instruction table.

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

+6
java branch jump-table
source share
9 answers

Java has a switch statement , but whether it compiles the bytecode conversion table depends on the implementation. In general, compilers will build you a conversion table if they find good constants for each case in the switch statement. I'm not sure you don't care how this is implemented. If you code in Java first, you are probably just fine at letting the compiler and JIT take care of these things for you.

Note that the switch only works with integer primitive types and enumerations, so you should use if / else statements if you use other types of objects (and you probably shouldn't compare doubles or float for equality anyway).

Finally, although enumeration references are technically “persistent”, some compilers will only generate a jump table when enums is enabled if the switch statement is in the same compilation unit where the enumeration is defined. Otherwise, it will create an if / else chain for you (as you have to do for regular objects). For more detailed information, see the java.net forums for expanding switch usage for objects .

+12
source share

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

I think case arguments (with switch in java) are equivalent.

In addition, in the OOP, the switch can be encoded once, and then allow the polymorphism to the job.

From: http://www.refactoring.com/catalog/replaceConditionalWithPolymorphism.html

alt text

It depends on your requirements.

+4
source share

A term used to describe an effective method of transferring program control (branching) to another part of a program (or another program that can be dynamically loaded) using a jump instruction table.

One way to transfer program control is to call a function. One way to call the correct function when you have several to choose from is to separate it from the type of object. This is called polymorphism. Java and other object-oriented languages ​​have polymorphism implemented through inheritance (subclassification). You do not know how it is implemented in Java, but in C ++ there is (usually? Or always?) A pointer in each object that points to a v-table for it of a class that contains pointers to virtual functions.

I seriously doubt that the absence of a custom transition table will damage the performance of your Java application.

+3
source share

I don't think you need such performance hackers in Java. First, I would like to concentrate on writing readable code and using decent algorithms - this will bring more performance benefits than what you are discussing.

In most standalone applications, the vast majority of the time is spent sitting, waiting for the user to do something. In most web applications, the bytecode runtime in the JVM must be loaded by network time, database time, or business logic.

If you really care about the performance of part of your Java application, you can move it to JNI code and generally bypass the Java interpreter.

+1
source share

Yes, absolutely.

If you code the switch statement, depending on various things, the switch is converted to instructions for using tables in bytecode. Generally

  • The switch must depend on the value of int
  • The highest int value and the minimum int value should not be too far apart.

The easiest way to achieve this is to use java enumerations, which are specially processed by the compiler. Related documentation is in the Java Virtual Machine Specification . Of course, the JIT compiler will almost certainly convert them directly into very fast switches into machine code of any platform on which you work.

Having said that, the real answer to your question is: "This is what you worry about when you are making machine code, and not programming in a high-level language."

+1
source share

I think some switch statements "under the hood" say so.

Other than that, you can do something similar with something like HashMap<whatever, Method> , where you use map.get(something).invoke() . But this view defeats the goal, because it will not be as fast as the transition table, and I cannot come up with a good case when OOP programming / polymorphism will not do the job better and cleaner.

0
source share

The branch / branch tables you are talking about are not directly provided by high-level languages ​​such as Java, C, etc., but are generated by the compilers into machine code or byte code. In other words, your compiler can use them, but you do not see them.

0
source share

You can use enum for 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(); 
0
source share

You can do this with reflection and a generic HashMap that stores anonymous inner classes, but that would be a terrible hack.

This is really elegant to do in C # because of its own anonymous methods, but not in java.

-one
source share

All Articles