|
Java™ Virtual Machine Support for Non-Java Languages |
The Java language and hence the Java Virtual Machine (JVM) enable the development of applications that
The JVM in JDK 7, allows dynamically typed non-Java languages to also leverage the infrastructure and performance optimizations of the JVM
Java is a statically typed language, meaning, all type information for variables, method parameters, return values etc. is available when a program is compiled. The Java compiler uses this type information to produce optimized bytecode which can then be efficiently executed by the JVM at runtime.
The following example of a Hello World program demonstrates static typing. Types are shown in bold.
import java.util.Date;
public class HelloWorld {
public static void main(String [] args) {
int len = args.length;
String hello = "Hello ";
Date currDate = new Date();
for (int i = 0; i < len; i++) {
System.out.println(hello + args[i]);
System.out.println("Today's date is: " + currDate);
}
}
}
Dynamically typed languages such as JavaScript and Ruby, typically do not have any type information available at compile time. The "type" of an object can only be determined at runtime. Hence, in the past, they could not be efficiently implemented on the JVM. Below is a snippet of the Hello World program written in Ruby.
#!/usr/bin/env ruby
require 'date'
currDate = DateTime.now.to_s
hello = "Hello "
ARGV.each do|a|
puts hello + "#{a}"
puts "Date and time: " + currDate
end
The invokedynamic instruction introduced in JDK 7 simplifies the
implementation of dynamically typed languages on the JVM. This instruction
provides user-definable or pluggable linkage behavior. In other instructions,
class based or interface based linkage behavior is hard-wired by the JVM.
The invokedynamic instruction uses the new bytecode-point 186.
The format is similar to invokeinterface, but omits the target
class, and includes just method name and descriptor.
Language implementors should use the API in the java.dyn package
to specify the invokedynamic instruction.
The java.dyn package defines the Java level APIs necessary to
define and manage the "target method" of each invokedynamic
call site. This API
includes a new type called MethodHandle which is the target of an
invokedynamic call.
| Copyright
©2009 Sun Microsystems, Inc. All Rights Reserved.
Feedback |
|