CSC/ECE 517 Fall 2013/ch1 1w03 ss: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
 
(13 intermediate revisions by 2 users not shown)
Line 4: Line 4:
== Background ==
== Background ==


<ref>http://www.drdobbs.com/jvm/a-long-look-at-jvm-languages/240007765</ref>Over the last 10-15 years , the [http://en.wikipedia.org/wiki/Java_virtual_machine 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 the other advanced features, providing runtime environments to the other languages.
<ref>http://www.drdobbs.com/jvm/a-long-look-at-jvm-languages/240007765</ref>Over the last 10-15 years , the [http://en.wikipedia.org/wiki/Java_virtual_machine 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 ?
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.
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 the JVM’s advanced [http://en.wikipedia.org/wiki/Just_in_Time 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 .
Additionally , with JVM’s advanced [http://en.wikipedia.org/wiki/Just_in_Time 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.
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.
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 the 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.
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==
==Languages Built on JVM==
Line 30: Line 30:
==JRuby==
==JRuby==


JRuby<ref>http://www.jruby.org</ref> is an implementation of the Ruby programming language built on the  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.
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====  
====History of JRuby====  
=====Developers=====  
=====Developers=====  
Line 52: Line 52:
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 [https://netbeans.org/ 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.
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 [https://netbeans.org/ 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.


<b>Code Snippet</b> <br>
<b>Code Snippet:</b> <br>
JRuby Calling Java
JRuby Calling Java


Line 65: Line 65:
====Advantages of JRuby over Java====
====Advantages of JRuby over Java====
• Ruby has features missing from Java  
• Ruby has features missing from Java  
* Blocks also known as Closures
* Blocks also known as Closures


Line 78: Line 79:
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).  
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  
*On average JRuby, runs 2 and a half times faster than Ruby, except at startup.


*In addition to native threads, JRuby supports Unicode natively
*In addition to native threads, JRuby supports Unicode natively.


==Jython==
==Jython==
Line 113: Line 114:


*It also supports a full object-oriented programming model .
*It also supports a full object-oriented programming model .
<b>Code Snippet:</b><br/>
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==
==Clojure==


<ref>http://en.wikipedia.org/wiki/Clojure</ref>[http://clojure.org/ 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 the Java Virtual Machine, Common Language Runtime, and JavaScript engines.  
<ref>http://en.wikipedia.org/wiki/Clojure</ref>[http://clojure.org/ 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.
'''Licensing''': Eclipse Public License.
Line 139: Line 155:


*In-built concurrency.
*In-built concurrency.
<b>Code Snippets: </b><br/>
Hello World in Clojure
  (println "Hello world!")
Defining a square function
  defn square [x]
  (* x x))


==TCL/Java==
==TCL/Java==
Line 196: Line 221:


*metaprogramming.
*metaprogramming.
<b>Code Snippet:</b><br/>
Example for Metaprogramming in Groovy:
  Number.metaClass {
  sqrt = { Math.sqrt(delegate) }
  }
  assert 9.sqrt() == 3
  assert 4.sqrt() == 2


====IDE Support====
====IDE Support====


Many integrated development environments and text editors support Groovy:
Many integrated development environments and text editors support Groovy:
[http://www.eclipse.org/ Eclipse], through Groovy-Eclipse , Emacs, using Groovy-Mode , Grails/Griffon in the Ultimate Edition only , NetBeans, since version 6.5 , etc
[http://www.eclipse.org/ Eclipse], through Groovy-Eclipse, Emacs, using Groovy-Model, Grails/Griffon in the Ultimate Edition only, NetBeans, since version 6.5 , etc


==Tutorial Links==
==Tutorial Links==

Latest revision as of 20:24, 24 September 2013

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/>