Basic concepts and advantages of Jython
As previously mentioned, Jython is an implementation of Python written in the Java language and integrated with the Java platform. Python is a scripting language often used in UNIX-based systems, including Linux. Python was invented by Guido van Rossum and introduced to the developer community in 1991. Jython currently supports the Python syntax at level 2.1.
Unlike the Java programming language, Jython is a scripting language. A scripting language is generally defined as follows:
- Very easy to learn and code
- Expressive and concise, yet powerful
- Has minimal required structure to create a running "program"
- Supports interactive (command-at-a-time) execution
- Does not require a compile step
- Supports reflective programming
- Supports functional programming
- Supports dynamic execution of source (that is, an eval function)
- Runs external programs
In general, it can be said that scripting languages value programmer efficiency over machine efficiency and performance. Compared to a programming language such as the Java language, Jython is easy to learn and efficient to code.
Jython can also be described as an agile language. Agile languages are generally thought of as being capable of performing a wide variety of tasks and useful for many different types of problems, easy-to-use and yet powerful and expressive. They are also ideal rapid prototyping languages.
Like its C-based cousin Python, Jython is most at home when used to develop small programs and scripts; it has many features that allow simple but functional programs to be created in a few minutes. This does not mean Jython cannot be used for large-scale programming. In fact, Jython supports a sophisticated packaging scheme, similar to that of the Java language. By virtue of its object-oriented nature, Jython is highly extendable and provides the latest constructs for effective software engineering.
Like the Java language and unlike some other scripting languages such as Perl and Rexx, Jython was designed to be an object-oriented language from the start. Thus, it offers powerful object-oriented programming (OOP) features that are easy to understand and use.
One of Jython's biggest advantages is that it runs on any JVM, so applications coded in Jython can run on almost any computing system.
Jython is built on the Java platform. From the platform's point of view, the Jython runtime is just another Java class. This is quite apparent if you look into the JYTHON.BAT file, which launches the Java runtime with the Jython interpreter as its main class, as shown below:
@echo off rem This file generated by Jython installer rem JAVA_HOME=<java_home> rem rem collect all arguments into %ARGS% set ARGS= :loop if [%1] ==  goto end set ARGS=%ARGS% %1 shift goto loop :end %JAVA_HOME%\bin\java.exe -Dpython.home=C:\jython-2.1 -cp "C:\jython-2.1\jython.jar;%CLASSPATH%" org.python.util.jython %ARGS%
At its heart Jython is an interpreted language. In Jython, there is no pre-compile step as there is in Java and C++; each time Jython code is run it is interpreted afresh. As such, code changes can be very quickly made and tested. Jython code can also be entered interactively (that is, one line at a time). Furthermore, you can dynamically construct Jython code (that is, as a string) and execute it directly. This enables coding flexibility not possible in Java coding.
The Jython interpreter converts Jython
source into an internal form for more efficient processing.
It does this during a first pass that verifies syntax. Once
this pass is complete the internalized source is
interpreted. Jython also caches this internalized form on disk. In a Java class file for the Jython module
<name>.py, the cached file would be
Interpretation does have its disadvantages, although most are minor. For example, use of an undefined variable is not a compiler error, so it will be detected only if (and when) the statement in which the variable is used is executed. While this can seem a disadvantage when compared to compiled languages, the fact that you can edit and then immediately run a program and experience the error (if it exists) makes up for it. A simple test-and-debug procedure takes about as much time as repeated edit-compile steps do to remove an error.
Because Jython is interpreted, it can be slower than a compiled language such as Java. In most applications, such as scripts or GUIs, this difference is hardly noticeable. In most cases, Jython's increased design and coding flexibility more than makes up for any small performance loss.
Because Jython code is dynamically converted to Java byte code, the latest enhancements to the Java platform (such as JITs and Sun's HotSpot JVM) can also eliminate many performance issues.
For an additional performance boost it is possible to implement code sections in the Java language and call them from Jython. For example, you could prototype your programs in Jython, test them out, and (in the case of performance issues) convert the critical sections to Java code. This technique is a good combination of the powers of Jython and the Java language, as prototyping is much easier in Jython than in Java. We'll talk more about combining the Java language and Jython in Part 2 of this tutorial.