What's invokedynamic and how do I use it?

孤街醉人 提交于 2019-11-26 23:49:31

问题


I keep hearing about all the new cool features that are being added to the JVM and one of those cool features is invokedynamic. I would like to know what it is and how does it make reflective programming in Java easier or better?


回答1:


It is a new JVM instruction which allows a compiler to generate code which calls methods with a looser specification than was previously possible -- if you know what "duck typing" is, invokedynamic basically allows for duck typing. There's not too much you as a Java programmer can do with it; if you're a tool creator, though, you can use it to build more flexible, more efficient JVM-based languages. Here is a really sweet blog post that gives a lot of detail.




回答2:


Some time ago, C# added a cool feature, dynamic syntax within C#

Object obj = ...; // no static type available 
dynamic duck = obj;
duck.quack(); // or any method. no compiler checking.

Think of it as syntax sugar for reflective method calls. It can have very interesting applications. see http://www.infoq.com/presentations/Statically-Dynamic-Typing-Neal-Gafter

Neal Gafter, who's responsible for C#'s dynamic type, just defected from SUN to MS. So it's not unreasonable to think that the same things had been discussed inside SUN.

I remember soon after that, some Java dude announced something similar

InvokeDynamic duck = obj;
duck.quack(); 

Unfortunately, the feature is no where to be found in Java 7. Very disappointed. For Java programmers, they have no easy way to take advantage of invokedynamic in their programs.




回答3:


There are two concepts to understand before continuing to invokedynamic.

1. Static vs. Dynamin Typing

Static - preforms type checking at compile time (e.g. Java)

Dynamic - preforms type checking at runtime (e.g. JavaScript)

Type checking is a process of verifying that a program is type safe, this is, checking typed information for class and instance variables, method parameters, return values, and other variables. E.g. Java knows about int, String,.. at compile time, while type of an object in JavaScript can only be determined at runtime

2. Strong vs. Weak typing

Strong - specifies restrictions on the types of values supplied to its operations (e.g. Java)

Weak - converts (casts) arguments of an operation if those arguments have incompatible types (e.g. Visual Basic)

Knowing that Java is a Statically and Weakly typed, how do you implement Dynamically and Strongly typed languages on the JVM?

The invokedynamic implements a runtime system that can choose the most appropriate implementation of a method or function — after the program has been compiled.

Example: Having (a + b) and not knowing anything about the variables a,b at compile time, invokedynamic maps this operation to the most appropriate method in Java at runtime. E.g., if it turns out a,b are Strings, then call method(String a, String b). If it turns out a,b are ints, then call method(int a, int b).

invokedynamic was introduced with Java 7.



来源:https://stackoverflow.com/questions/6638735/whats-invokedynamic-and-how-do-i-use-it

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