CSC/ECE 517 Fall 2013/ch1 1w03 ss

From Expertiza_Wiki
Jump to navigation Jump to search

Introduction to JRuby and other such languages

Background

<ref>http://www.drdobbs.com/jvm/a-long-look-at-jvm-languages/240007765</ref>Over the last 10-15 years , the Java Virtual Machine(JVM) has been a popular platform to host languages other than Java. This can be attributed to the performance and “write-once-run-anywhere” portability of the JVM , as well as other advanced features, providing runtime environments to the other languages.

Why have so many languages, including Ruby, Groovy,and Python, been ported to Java ?

The main reason is because its easier to target one platform and rely on the multi platform JVM to host it than it is to write interpreters for each of the operating system.

Additionally , with JVM’s advanced Just In Time (JIT) compilation ,the resulting compiled and optimized Java bytecode will typically run with equal or better performance than naive interpreters. Further , the Java JIT compiler continues to optimize code well after it’s first complied ,depending upon changes in code execution and branching. The cost and effort associated with building this into each independent interpreter makes this kind of performance prohibitive; so , it makes sense to leverage the JVM’s existing multi platform implementation of this performance optimization feature .

A list of other features that JVM brings along are a huge set of well tested libraries , garbage collection , excellent built-in tools and debugging interfaces that a language developer can easily plug into. The biggest advantage that JVM brings to the table is interoperability that it provides.

Prior to Java Se 7 2011 , there was a small penalty hit when crossing the barrier to and from Java code . This was because JVM was not initially built to support dynamically typed languages. To fix this Java Specification Request 292 was developed with the goal of creating a new bytecode instruction to resolve this issue. The result is invoked dynamic bytecode, which is an enhancement to the JVM that allows dynamic and direct linkage between the calling code and receiving code at run time.

Languages Built on JVM

  • JRuby
  • Jython
  • Clojure
  • TCL/Java
  • Groovy

JRuby

JRuby<ref>http://www.jruby.org</ref> is an implementation of the Ruby programming language built on Java Virtual Machine, written in Java. JRuby is tightly integrated with Java so as to provide full two-way access between the Java and the Ruby code.

History of JRuby

Developers
  • Charles Oliver Nutter
  • Thomas Enebo

It was originally created by Jan Arne Petersen, in 2001.

JRuby 1.1 added Just-in-time (JIT) compilation and Ahead-of-time (AOT) compilation modes to JRuby and was already faster in most cases than the then-current Ruby 1.8.7 reference implementation.

JRuby packages are available for most platforms; Fedora 9 was among the first to include it as a standard package at JRuby 1.1.1 Recently , JRuby has been gaining increased presence in the Java and Ruby communities.

Commercial Support for JRuby : Engine Yard Inc.

Compilation of JRuby code is mixed mode which means that the code can be interpreted ,JIT (Just in time ) compiled or AOT (Ahead of time) compiled.

JRuby was the driving force behind adding the invoke dynamic bytecode to the Java SE 7 release. This facilitates Java-to-Ruby and reverse calls to be treated the same as Java-to-Java calls. Additionally, JRuby inspired Sun's Multi-VM JVM research effort, where one JVM can act as a separate sandbox to each of multiple applications, allowing different versions of the JRuby runtime to be loaded and active at the same time.

Framework Support

JRuby has built-in support for Rails, RSpec, Rake, and RubyGems. It allows the use of bundled C libraries known as gems. It also provides the feature of launching the Interactive Ruby Shell (irb). The Netbeans Ruby Pack, available since NetBeans 6.0, allows IDE development with Ruby and JRuby, as well as Ruby on Rails for the two implementations of Ruby.

Code Snippet:
JRuby Calling Java

 require 'java'

 frame = javax.swing.JFrame.new
 frame.getContentPane.add javax.swing.JLabel.new('Hello, World!')
 frame.setDefaultCloseOperation javax.swing.JFrame::EXIT_ON_CLOSE
 frame.pack
 frame.set_visible true}}

Advantages of JRuby over Java

• Ruby has features missing from Java

  • Blocks also known as Closures
  • Open classes
  • Meta programming

• Domain Specific Language (DSL)

Advantages of JRuby over Ruby

JRuby provides you with the best of both the Java and Ruby world : Ruby applications and libraries, plus Java libraries. And you can access those libraries with Ruby syntax (or Java syntax, if you want).

  • On average JRuby, runs 2 and a half times faster than Ruby, except at startup.
  • In addition to native threads, JRuby supports Unicode natively.

Jython

<ref>http://www.jython.org</ref>Jython is the successor to JPython. The Jython project is in accordance with the JPython 1.1.x license. The intent is to manage this project with the same open policies that are serving CPython so well.

What is Jython?

JPython is an implementation of the Python programming language designed to run on the JVM. It consists of a compiler to compile Python source code to Java bytecodes which can run directly on a JVM, a set of support libraries which are used by the compiled Java bytecodes, and extra support for the usage of Java packages from within JPython. JPython has been renamed as Jython.

Project inception: Created by Jim Hugunin in 1997
Licensing: Python Software Foundation license

Jython is a fully dynamic language system.

Compilation of Jython is mixed mode which means that the code can be interpreted , JIT compiled or AOT compiled.

Jython, is one of the very first languages to be ported on to the JVM, which is a Java implementation of the Python interpreter to facilitate high-performance Python application execution. Written on the JVM, Jython supports calls into Java code and libraries, and all code is JIT compiled at runtime. The startup time and performance of Jython code is largely determined by the JVM. Jython is almost as fast as CPython.

Purpose

<ref>https://wiki.python.org/jython/JythonFaq/GeneralInfo</ref>There are numerous alternative languages implemented for the Java VM. The following features help to separate Jython from the rest:

  • Dynamic compilation to Java bytecodes - provides highest possible performance.
  • One can extend existing Java classes in Jython - providing effective use of abstract classes.
  • Jython allows creation of applets, servlets and beans.
  • Python Language - combines remarkable power with clear syntax.
  • It also supports a full object-oriented programming model .

Code Snippet:
A class declaration in Jython :

class Hello:

    def __init__(self, name="John Doe"):
       self.name = name
    def greeting(self):
       print "Hello, %s" % self.name

Creation of objects :

   jane = Hello("Jane Doe")
   joe = Hello("Joe")
   default = Hello()

Clojure

<ref>http://en.wikipedia.org/wiki/Clojure</ref>Clojure is a dialect of the Lisp programming language. It was created by Rich Hickey. It is a functional general-purpose language. Its focus on programming with immutable values and explicit progression-of-time constructs .With the help of these constructs it provides support for multi-threaded programs. Clojure runs on Java Virtual Machine, Common Language Runtime, and JavaScript engines.

Licensing: Eclipse Public License.

Commercial support: Community-based

Language type system: Strong, dynamic

Compilation: Bytecode, and JIT compiled

Clojure is a modern-day functional programming language, inter-operable with object-oriented code, and built for concurrency. It's built on and tightly integrated with the JVM, treats all code as data, views functions as objects, and emphasizes on recursion . The main feature of Clojure is its immutable state system that provides much easier, less error-prone, parallel code execution.

Advantages

<ref>http://stackoverflow.com/questions/3046769/advantages-of-clojure</ref>*It provides all the benefits of functional programming languages.

  • It allows dynamic, compact code with late binding, macros, multimethods.
  • Can code functions to sequence abstraction instead of to specific data structures
  • In-built concurrency.

Code Snippets:

Hello World in Clojure

 (println "Hello world!")

Defining a square function

 defn square [x]
 (* x x))

TCL/Java

<ref>http://en.wikipedia.org/wiki/Tcl/Java</ref>Tcl/Java , a project to bridge Tcl and Java, consists of two distinct packages:

  • Tcl Blend
  • Jacl

TCL Blend

Tcl Blend is a Tcl extension that makes use of JNI to facilitate communication between a Java interpreter and a Tcl interpreter. Tcl Blend loads a Java interpreter into an existing Tcl process. Here, the behavior that has been implemented in Java is accessed in a Tcl script. For example, A Java object allocated in a Tcl script can interactively invoke Java methods on this object.

Loading Tcl Blend and Tcl into a Java process enables one to add scripting functionality to an existing Java application. TCL Blend can be used with other popular Tcl extensions like Tk.

JACL

JACL is an implementation of a Tcl interpreter. It is written entirely in Java. Jacl facilitates communication between a Java interpreter and a Tcl interpreter. Jacl helps in incorporating Tcl scripting into an existing Java application, without dealing with the complexities that come with loading Tcl Blend into a Java process.

JTcl, a fork of Jacl, aims to continue the modernization of Jacl that began with the Jacl Modernization Project in the Google Summer of Code.

Groovy

<ref>http://en.wikipedia.org/wiki/Groovy_(programming_language)</ref>Groovy, an object-oriented programming language for the Java platform, is a dynamic language with features similar to those of Python, Ruby and Perl. Most Java code is also syntactically valid Groovy.

  • Groovy can be used as a scripting language for the Java Platform.
  • Groovy is dynamically compiled to Java Virtual Machine (JVM) bytecode
  • Groovy interoperates with other Java code and libraries.

History

James Strachan first talked about the development of Groovy in his blog in August 2003. Several versions were released between 2004 and 2006. After various betas, Groovy 1.1 Final was released and immediately rebranded as Groovy 1.5 as a reflection of the many changes that were made.

Features

<ref>http://stackoverflow.com/questions/764416/why-would-one-use-groovy-over-java</ref> Most valid Java files are also valid Groovy files. This facilitates Java programmers to easily switch to Groovy. Groovy code is more compact when compared to Java. This is because it does not require all the elements that Java requires.

Groovy features not available in Java include:

  • both static and dynamic typing
  • closures
  • operator overloading
  • native syntax for lists and associative arrays
  • support for regular expressions
  • expressions embedded inside strings(String interpolation)
  • additional helper methods
  • Mixins/Categories
  • Collection literals
  • metaprogramming.

Code Snippet:
Example for Metaprogramming in Groovy:

 Number.metaClass {
 sqrt = { Math.sqrt(delegate) }
 }
 assert 9.sqrt() == 3
 assert 4.sqrt() == 2

IDE Support

Many integrated development environments and text editors support Groovy: Eclipse, through Groovy-Eclipse, Emacs, using Groovy-Model, Grails/Griffon in the Ultimate Edition only, NetBeans, since version 6.5 , etc

Tutorial Links

References

<references/>