Loading...

What is Jython? How to use it to develop a small project?

What is Jython? How to use it to develop a small project?

As easy as it sounds, Jython is basically Java + Python. Among the top implementations of the popular language Python, Jython has its own advantages and limitations. Let’s discuss in this article what Jython actually is and build a small project to understand better.

Python is slow

My previous article discusses in depth that Python is very slow because it is a dynamically typed interpreted language. Codebases of languages faster than Python increase their performance either by compiling their code or adding a fixed typing system or both. Popular implementations of Python such as Cython add static typing and compile the Python code using their compilers, hence increasing their performance.

Jython aims to achieve the same by implementing Java code directly into Python. Jython increases the performance of Python projects by adding support for Java libraries and codebases created in Java. This gives to-and-fro support between Java and Python codebases.

What is Jython?

As the official documentation of Jython say, “Jython is an implementation of the high-level language, dynamic, object-oriented language, Python that can seamlessly be integrated with Java Platform.”

Java vs Python

Python is among the top language for beginners to learn and also for professional developers to build complex applications using it. It has a simple and shorter code syntax but is much slower than other languages. It has dynamic typing, which makes the interpreter deal with more complexities while handling memory.

On the other hand, Java has a much larger syntax but has much stronger memory management than Python. Java is platform-independent and can run on any type of machine due to the JVM (Java Virtual Machine). This makes Java more portable than Python. Java is much faster than Python and the compiled java code can run on different platforms without recompilation.

Jython – The best of both worlds

Jython is an implementation of Python which integrates Java applications and libraries into Python projects. It adapts to the shorter syntax of Python while using all the popular libraries in Java such as Solaris, Mallet, Deeplearning4j, etc. Its codebase uses libraries compiled to byte code through dynamic compilation leading to higher performance without sacrificing interactivity. Jython also gives the ability to extend Java classes allowing the effective use of abstract classes.

Advantages of Jython

As the syntax of Jython is the same as the syntax of Python, Jython inherits all the advantages of Python’s syntax style:

  • Easy to read
  • Clear and concise
  • Dynamically Typed
  • Beginner Friendly
  • Versatile

An additional benefit that Jython has over Python is that Jython does not inherit the Global Interpreter Lock that the latter has. Python was not truly efficient due to the Global Interpreter Lock (GIL). In multi-threading, only one thread was run at a time. Jython not having the GIL means that it is capable of using multi-threading to its full potential.

Installing Jython

First, download and install JDK (Java Development Kit) which lets you run Java as well as Jython and helps install Jython too. Use this link to download and install the JDK on different machines: https://www.oracle.com/in/java/technologies/downloads/.

Then, download and install Jython via their official website: https://www.jython.org/download and install it using the command:

java -jar jython-installer-2.7.2.jar
Code language: Bash (bash)

Or use the package managers in Unix-based systems like Ubuntu using:

sudo apt install jython
Code language: Bash (bash)

You can find out more about this installation procedure on their official website: https://www.jython.org/installation.html.

How to use Jython?

Jython can easily integrate Java applications inside a Python project. Its syntax is the same as Python’s. Jython implements all the Java libraries and custom-created modules via this Python code itself without any other external APIs or tools.

Jython works both ways, not only Java code is being integrated by Jython, but also Python code can be run in Java using Jython.

Building a project with Jython

As the codebase is the same, let’s implement the most famous sorting algorithm Bubble Sort. ArrayLists are data structures unique to Java only. For the sake of implementing Java libraries, we will be using ArrayLists in Jython to implement Bubble Sort.

# bubble_sort.jpy from java.util import ArrayList def bubble_sort(arr): n = len(arr) for i in range(n): for j in range(n-i-1): if arr[j] > arr[j+1]: arr[j], arr[j+1] = arr[j+1], arr[j] return arr def main(): arr = ArrayList([9, 6, 7, 8, 3, 5, 1, 4, 2, 9, 10, 4, 5, 7]) result = bubble_sort(arr) print(result) if __name__ == "__main__": main()
Code language: Python (python)

To run the above code, use the following command:

jython bubble_sort.jpy
Code language: Bash (bash)

Note that the extension used here “.jpy” is just a random extension as there is no fixed extension for jython code files.

# Result: [1, 2, 3, 4, 4, 5, 5, 6, 7, 7, 8, 9, 9, 10]
Code language: Bash (bash)

Limitations of Jython

  • Jython is slower than other implementations of Python like Cython, IronPython, etc. It is slower than other languages in general. This is because Jython is still an interpreted and dynamically typed language. The codebase does not achieve optimum performance if the larger part is built using native Jython and not integrated via Java modules and libraries.
  • Lost majority community support, due to rising competition.
  • Not as useful as the other implementations of Python.

Conclusion

Jython does not require anything extra and if you have learned Python, you know Jython as well. This article only showcases very little of what Jython can do. It is fun to check it out and build small user-friendly apps. But it is not recommended to be implemented in large-scale projects as there is not much support available to Jython and lags far behind compared to other implementations like Cython.

Sharing is caring

Did you like what Aman Ahmed Siddiqui wrote? Thank them for their work by sharing it on social media.

0/10000

No comments so far