Is there anything like branch/jump table in Java?

三世轮回 提交于 2019-12-08 19:47:46

问题


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?


回答1:


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.




回答2:


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

I think case statements ( with the switch in java ) is the equivalent.

Additionally in OOP the switch could be coded once and then let polymorphism to the job.

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

It depends on your requirements.




回答3:


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.




回答4:


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".




回答5:


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.




回答6:


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.




回答7:


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.




回答8:


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();



回答9:


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.



来源:https://stackoverflow.com/questions/548053/is-there-anything-like-branch-jump-table-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!