Imagine a bacon-wrapped Ferrari. Still not better than our free technical reports.
See all our reports

5 reasons developers learning Java might like Jython

jython

You’ve probably heard of Jython before–a 13-year-old, mature, JVM-based, object-oriented scripting language, Jython is the ‘Java’nised version of Python, if you will. In the past, it went by the name JPython, a far cooler name, if you ask me!

Considering the time and effort that has been put into it in the recent years (the language development had a hiatus period during 2005- 2008/ 09); it has still not received the amount of attention from the programming community that it should have. This is one language that is laden with features and is the best of both worlds: Java and Python.

Agreed, that there are a lot (I mean, A LOT) of JVM languages on the horizon as of now which have taken the world by storm in the recent years: Clojure, Scala, Groovy, JRuby to name a few.

Then, a very obvious question comes to mind: Why should a developer (especially the ones who’ve already fiddled a lot with Java or Python or both) try it out? Does it possess some kind of special powers (i.e. one lang to rule them all?)

Does the world really need another language based on the JVM? Despite Jython being one of the older and more mature of the JVM languages, a recent outburst in this category is one of the major reasons why it has become all the more difficult on the part of a programmer to judge which new language they should try on a future project. And this article is aimed at bridging this very canyon.

For the record, I see the following 5 benefits of learning Jython, which I’ll outline throughout this article and explain more in detail at the end:

  1. Reusability of Java features
  2. Java bytecode compilation
  3. The syntax simplicity of Python
  4. Creation of applets, frames and so on possible from within
  5. Doubles up as an HLL (high level language) and an excellent embedded scripting language

Before we get into that, I’d like to explain where I’m coming from, what I love about both Java and Python and why Jython is a great pact between the two.


What I love about Java

Aah, Java. One of the most-beautiful, nearly-OO languages created. Everything has to be strictly inside a class (or an interface or an enum), and that is where it scores massively over C++, another widely-used and time-tested programming language. Plus a few other features, such as bytecode generation (duh!) and better abstraction, to name a few.

Java started early in the last decade of the 20th century, coinciding with the rise of the internet and was thus considered by many as one of the major catalysts in the early years of the new millennium. It’s been almost 20 years of Java not looking back and still going strong. There have been a lot of frequent updates to the JDK, the latest being Java 8 (or version 1.8), whose main feature was the introduction of lambda expressions. Java is now a step closer to the functional programming paradigm with Project Nashorn, improved Date and Time API, and a lot more.

As concerns its syntax, it is very nearly OO (“very nearly”, because of the persistence of the primitive data types like int, char, double, float, even though their OO counterparts: wrapper classes exist.) The syntax is not too complex and not too difficult to be understood by someone who is fairly comfortable with the rules of the object-oriented approach. What’s it look like?

“Hello World!” in Java

 

public class HelloClass {
	public static void main(String[] array) {
		System.out.println("Hello World!");
	}
}

Just keep the name of the file as “HelloClass.java”, compile it into the class file (HelloClass.class) by using the command:

> javac HelloClass.java

and run it as using the following command:

> java –cp . HelloClass


What I love about Python

 
Python is an OO scripting language which has garnered a legion of dedicated followers ever since its inception. The reason: it’s easy to write & understand, and it scales. According to a recent survey conducted by the Association for Computing Machinery (published 07/07/2014), Python has overtaken Java to emerge as the top language choice taught in introductory computer science courses across the United States. The major advantage: its syntax. Consider the code for displaying the string “Hello World!” over on the console. C first:

“Hello World!” in C:

 

#include<stdio.h>
#include<conio.h>
int main()
{
	clrscr();
	printf("Hello World!");
	getch();
	return 0;
}

“Hello World!” in Python:

 
>>> print ('Hello World!')

That’s it. That is all there is to it. I am not trying to be discourteous to C (like anyone ever can!), but it so happens that making an introductory student aware of the complex features of the language like the inclusion of header files, the parameters to be passed to various functions, the role of getch(), the need to return the integer 0 at the end of a successful execution of the code is a rather difficult task as compared to the simple, straightforward syntax of Python.


What’s so great about a marriage between Java and Python?

 
As with every two programming languages taken together at a time, Java has some characteristics which Python does not (extensive libraries for various purposes and built-in support for many functionalities) and some Python has which Java doesn’t (an easily-understandable syntax to begin with, scalability). Considering the extensive feature sets of both the languages, it is prudent to think what can be achieved by their amalgamation: a lot of features and syntax simplicity, both at the same time. This is essentially Jython.

As a Python implementation written in Java, Jython offers all that Python offers and almost all that Java does as well, like database connectivity, cryptographic features, GUI among a lot of others. The developer may write Jython components and modules which can later be integrated with/ into a Java application, if desired.

You all remember JFrame from school, right? Consider this very simple piece of Jython code, which displays a simple frame with a title:

>>> import javax.swing as swing
>>> f = swing.JFrame(title = “My First Jython Frame”, visible = 1, size = (400, 400))

And presented below is the Java implementation of the same. Observe the differences in the codes for yourself:

import javax.swing.JFrame;
public class MyFrame extends JFrame {
	public MyFrame(String title) {
		this.setTitle(title);
		this.setSize(400, 400);
		this.setResizable(false);
	}
public static void main(String[] args) {
		new MyFrame("My First Java Frame").setVisible(true);
	}
}

Why should I try Jython?

 
In the introduction, I offered 5 reasons why I think Java developers would enjoy Jython. Here is where I explain myself a bit more as to why Jython should be considered a serious contender in the programming arena:

  1. Reusability of Java features: The usability of Java classes and interfaces in your Jython code is one of the best deals available. Also, other features which make Java stand apart from the crowd, such as multithreading and automatic garbage collection, enhance the feature set of Jython even more.
  2. Java bytecode compilation: One of the biggest strengths of Java that amazed the programming fraternity when it was introduced was the concept of bytecode generation in the form of a *.class file- a sweet piece of undecipherable text generated for every class and interface present inside your Java file. Apart from lending a hand in making the language portable across various platforms, it also improves performance as well. And all this is for free in Jython!
  3. The syntax simplicity of Python: >>> print ('Hello World!')
  4. Creation of Applets and Frames possible from within: Applets, though not as popular now as they used to be in the good ol’ days, can still be created using Jython. Creation of frames is also a piece of cake, as described by the example above.
  5. Doubles as a HLL (high level language) and an excellent embedded scripting language: Want to implement a dictionary, using a GUI to insert words just by clicking a button on a frame and an auto-updated text field which gives you the suggestions as you type? Or do you need a small piece of code, a method or a script maybe, which will initiate a call to the database from where the words are to be fetched, when the frame mentioned above is instantiated? Well, look no further! Jython’s here.

I hope you found my preliminary ideas about the usefulness of Jython for Java developers to be interesting. I’m currently learning a lot about programming and trying to figure out what parts of JVM fit me the best, so any advice or commentary you can leave below in the comments (or on Twitter @SabhyaKaushal) would be great!


Interesting in learning more about JVM languages? Check out the recent RebelLabs report The Adventurous Developer’s Guide to JVM Languages in which we look at Java, Scala, Groovy, Fantom, Clojure, Ceylon, Kotlin and Xtend

Next: 8 JVM languages reviewed!

 

  • Tharek

    I agree that Jython may be interesting, but these unfair examples of equivalent codes bother me.

    The correct C equivalent of print(“Hello world!”) is more like

    #include
    int main()
    {
    printf(“Hello World!”);
    return 0;
    }

    There is nothing close to clrscr() and getch() in this line of Python code.

    Also, in the second example, a closer equivalent Java program would be

    import javax.swing.JFrame;
    public class MyFrame {
    public static void main(String[] args) {
    JFrame f = new JFrame();
    f.setTitle(“My First Java Frame”);
    f.setSize(400, 400);
    f.setVisible(true);
    }
    }

    The comparison is clear enough as it is, no need to make Python/Jython opponents uglier than they need to be. ;)

  • Guest

    Hello Tharek. Thanks for letting me know your thoughts on the article. But I am afraid that while quoting these examples I had a completely different point of view, contrary to what you said about having made the Python code look uglier.

    Let’s first go to the C code. The statements: clrscr(); and getch(); serve a much bigger
    purpose. clrscr(); was placed in the code in order to have to import another *.h file (pointing towards the necessity to include a separate header file for this functionality and to clear the screen, of course), and it won’t be possible to actually see “Hello World!” on the console without a “getch();” when the statement says hello to you and runs back to the editor in a jiffy, would it?

    The second example: the JFrame creation code. Inheriting a super class and calling the required methods to instantiate the created object’s members from within the
    constructor, happens to be closer to the OO approach, something which Java can boast of.

    I am not trying to make C, or Python, or Java (whom I have worked with for a long time now) look bad in front of Jython, a language I have just started exploring. It just so happens that Jython merges the best of both Java and Python, and that’s the concept being explored via this article. Any indication otherwise might be ascertained to the limited knowledge set of the author. :P

    Thanks again for bringing these issues to light.

  • Sabhya Kaushal

    Hello Tharek. Thanks for letting me know your thoughts on the article. But I am afraid that while quoting these examples I had a completely different point of view, contrary to what you said about having made the Python code look uglier.

    Let’s first go to the C code. The statements: clrscr(); and getch(); serve a much bigger purpose. clrscr(); was placed in the code in order to have to import another *.h file (pointing towards the necessity to include a separate header file for this functionality and to clear the screen, of course), and it won’t be possible to actually see “Hello World!” on the console without a “getch();” when the statement says hello to you and runs back to the editor in a jiffy, would it?

    The second example: the JFrame creation code. Inheriting a super class and calling the required member methods to create the object from within the
    constructor, happens to be closer to the OO approach, something which Java can boast of.

    I am not trying to make C, Python, or Java (whom I have worked with for a long time now) look bad in front of Jython, a language I have just started exploring. It just so happens that Jython merges the best of both Java and Python, and that’s the concept being explored via this article. Any indication otherwise might be ascertained to the limited knowledge set of the author. :P

    Thanks again for bringing these issues to light.

  • David Leppik

    Where I work (vocalabs.com), we used Jython/Python as the primary scripting language for years. Easy stuff could be done in Python (and load/run fast) while fancier stuff would have access to all our Java libraries.

    Then around 10 years ago, the Jython project nearly died, and we lived in fear that we’d be held back by needing to support a dead platform. And there weren’t many alternatives.

    Then just as suddenly, interest in JVM scripting languages blossomed with Groovy and Scala, and newer versions of the JVM included features (e.g. invokevirtual) to support scripting. Soon Jython was back in the game, but not until after we decided to standardize on Scala.

    We’re staying with Scala because with a few lines of boilerplate we can migrate a script into a class in our main code tree. So we can convert a one-off project into core, reusable features.

    These days Jython is a safe choice, and probably the best choice if you have sysadmins who are already writing Python scripts.

  • Indian

    good one

  • James Lu

    OO is not purely good. We use OO for its benefits, not strictly for the sake of it. You believe in a God for the sake of believing in a God, but you use a vacuum for the purpose of cleaning. That doesn’t mean a more pure vacuum means a better cleaning tool, it might be easier to combine tools.

    And you know how Python is slow? Jython is *even slower*, despite compilation.