Presented at JAX London 2013
Java is not the only language! The JVM is a wonderful common runtime for a wide variety of languages. By using the right language for each task, you can deliver higher quality applications in less time.
9. Table of contents
• How I became a polyglot programmer
• The case for polyglot programming
• Languages, JVM and polyglot apps
• Impedance mismatch
• Glue code
• Language boundaries
• Getting started
18. Why the JVM?
• Great runtime
• Portability
• Common underlying VM for lots of
languages
• Interoperability
• Java library for everything
19. Side note:
Don’t forget the CLR
• Good stu" from Microsoft
• Great runtime
• DLR
• APIs
• Di"erent culture
• Trap?
20. Side note:
Don’t forget IPC
• Go old school
• Separate processes
• Common protocol over sockets and
pipes
• Thrift, Google Protocol Bu"ers, Avro
• or text over HTTP: XML, JSON, etc
22. What’s the best language?
• No such thing as the “best” language
• Use the best language for solving
your problems
• Polyglot is about avoiding compromises
23. Language choice
• Don’t use two closely related languages
• Don’t forget the standard library
• And some have special features
eg. concurrency libraries
24. Pick one from each column
Static
Dynamic
Scripting / DSLs
Java
Ruby
JavaScript
Scala
Groovy
Ruby
Clojure
Groovy
Jython
25. Aims
• Work in harmony
• Choose most appropriate language for
each task
• Choose carefully as choices are hard to
undo
26. Memory usage
• Multiple runtimes
• Multiple standard libraries
• Cloud is memory constrained
37. Glue code
• Work around impedance mismatch
• Java: The new assembly language
• Rule enforcement & security
• Cheat until your pro!ler says it’s slow
• Serialise/deserialise to common format,
eg JSON
38. undefined is not null
JAVA
public class Host
extends ScriptableObject {
public String
jsFunction_f(String x)
{
System.out.println(
"x = " + x
);
}
}
39. undefined is not null
JAVA
public class Host
extends ScriptableObject {
public String
jsFunction_f(String x)
{
System.out.println(
"x = " + x
);
}
}
JAVASCRIPT
var h = new Host();
h.f("Hello");
h.f(2);
h.f(null);
h.f(undefined);
40. undefined is not null
JAVA
public class Host
extends ScriptableObject {
public String VALUES OF X
jsFunction_f(String x)
{
System.out.println(
"x = " + x"Hello"
);
"2"
}
null
}
"undefined"
JAVASCRIPT
var h = new Host();
h.f("Hello");
h.f(2);
h.f(null);
h.f(undefined);
47. Divided by a
common language
• JRuby: Transparent bridging
• JavaScript: Host objects + bridging
• Groovy: Java is subset of Groovy
• Scala: Write Java with Scala syntax
48. JSR-223
• Java Scripting API
• Common API for scripting languages on
the JVM
• Scripting “engines” available for many
languages, variable quality
• Su"ers from impedance mismatch itself
• Functions may return unexpected types
53. Ruby implementation
class Framework
def start_application
# ...
end
def handle_from_java(
servlet_request, application, body, is_ssl)
# magic goes here
end
end
54. JavaScript to Ruby
(via Java)
public interface AppWorkUnit
{
public int id();
public
public
public
public
}
Integer actionableById();
void setActionableById(Integer id);
String jsGetDataRaw();
void jsSetDataRaw(String data);
public boolean save();
public void destroy();
59. Hiring polyglots
developers
• Find people who can write code,
not people who can write Java.
• Test to see if programming is instinctive,
not syntax and trivia.
• Look for people who continuously learn,
not those who have learnt.
60. Finding a polyglot job
• Smaller companies, or smaller teams
within large companies
• Early stage projects, not maintenance or
enhancements
• Talk to really good recruiters
• Networking!
62. The Enterprise
• It’s just Java the JVM
• Implement a servlet
• Use tools like Warbler
• TorqueBox
• New job outside the Enterprise
63. Starting points
• Use JRuby to try out an API
• Use Ruby testing libraries to test your
Java
• Use Groovy to a make business rules
DSL
• Write a layer of a small project in a
dynamic language