JAVA DEVELOPMENT KIT 7
JAVA DEVELOPMENT KIT 7
Dynamic Language Support on the Java Virtual Machine
A new bytecode and a new linkage mechanism offer better support for dynamically typed languages.
J or interpreting it. This specification and its
ava Development Kit (JDK) 7 is a prototype
This is great news for application devel-
implementation made it much easier to Platform, Standard Edition (Java SE). JDK 7
of what could be in the next release of Java
opers. The flexibility offered by dynamic lan-
create applications that include both Java introduces several key features to improve the
guages, especially scripting languages, makes
code and scripting code. Although support performance, usability, and security of the
these languages particularly attractive for
for JSR 223 has stimulated the development Java platform. Of particular interest to Java
application prototyping and experimentation
of scripting engines for the JVM, developers developers is the addition of support for Java
as well as for applications that evolve rapidly.
of these scripting engines have faced a Specification Request (JSR) 292. The addition
This flexibility stems from dynamic typing.
troublesome obstacle. of a new Java bytecode, invokedynamic, for
A language that is dynamically typed verifies
When developers write engines that method invocation, and an accompanying
at runtime that the values in an application
compile dynamically typed languages to run linkage mechanism that makes use of a new
conform to expected types. By comparison,
in the JVM, they have to satisfy the require- construct called a method handle, will enable
a statically typed language such as the Java
ments of the Java bytecode the JVM executes. implementers of compilers for dynamically
programming language does most of its type
Until now, that bytecode has been designed typed languages such as JRuby and Jython to
checking at compile time, checking the types
exclusively for statically typed languages, generate bytecode that runs extremely fast in
of variables but not of values. The Java lan-
especially in its treatment of method invoca- the Java virtual machine (JVM).
guage also allows some dynamic type checking
tion. This design has been a pain point for As a result, you should see more of your
of values, especially receivers of virtual or
script engine developers generating byte- favorite dynamically typed languages avail-
interface method calls. But even these calls
codes for method invocation. able in the Java ecosystem, along with a
require a static type for the receiver.
It is easier to write code in dynamically
performance boost in code generated by
typed languages than in statically typed
BYTECODE REQUIREMENTS FOR
dynamically typed language compilers that
languages, because the former have more-
METHOD INVOCATION
A statically typed language does its type advantage of the new invokedynamic Java
run in the JVM. This article will help you take
permissive type matching rules and can
checking before execution, at compile time. bytecode instruction for the JVM and the new
perform many type conversions automati-
What this means for a method invocation is method handle linkage mechanism.
cally. Dynamic typing is also more flexible
than static typing, because it enables pro-
that the compiler, in addition to the bytecode
grams to generate or configure types based
it generates, needs to know the type of the
DYNAMICALLY TYPED LANGUAGES
on runtime data. These behaviors can help
value returned by the method as well as the
AND THE JVM
types of any receiver or parameters specified If you said that the JVM is a program that
you create applications more quickly than
you would when coding them in a statically
in the call.
executes Java programs translated into
Consider the following Java code snippet: machine-independent bytecode, your state- ment would be correct but incomplete. The
typed language.
String s = “Hello World”; JVM does execute Java programs translated
A PROBLEM FOR DYNAMICALLY
System.out.println(s); into bytecode, but it is not limited to han-
TYPED LANGUAGES
The first step in bringing dynamic lan-
dling translated Java programs. Increasingly,
Note that the type of parameter for the JVM implementations of dynamic languages
guages to the JVM was JSR 223: Scripting
method is known. It’s a String. The println() are becoming available. These include JRuby,
for the Java Platform, a specification that
method doesn’t return a value, but if the an implementation of the Ruby program-
defined an API for accessing Java code from
code example did call a method that does ming language; Jython, an implementation
code written in dynamic scripting lan-
return a value, the type of the return value of the Python programming language; the
guages. JSR 223 also specified a framework
would need to be specified. The receiver of Groovy scripting language; and Rhino, an
for hosting scripting engines in Java appli-
the call, System.out, also has a statically implementation of JavaScript.
cations. A scripting engine is a program that
executes scripting code, either by compiling
known type, java.io.PrintStream.