26

The standard answer is that it's useful when you only need to write a few lines of code ...

I have both languages integrated inside of Eclipse. Because Eclipse handles the compiling, interpreting, running etc. both "run" exactly the same.

The Eclipse IDE for both is similar - instant "compilation", intellisense etc. Both allow the use of the Debug perspective.

If I want to test a few lines of Java, I don't have to create a whole new Java project - I just use the Scrapbook feature inside Eclipse which which allows me to "execute Java expressions without having to create a new Java program. This is a neat way to quickly test an existing class or evaluate a code snippet".

Jython allows the use of the Java libraries - but then so (by definition) does Java!

So what other benefits does Jython offer?

4
  • 6
    Funny, I was going to ask "Why use Java when you could just use Jython?" Try doing Swing from Jython and see how pleasant it is: btn = JButton("Click me", actionPerformed = SomeFunction). Bit easier than having to write an entire class just to handle button clicks, no? Commented Oct 3, 2011 at 23:09
  • 1
    Too bad that this question is closed; as there is a pretty good point which didn't show up in any of the answers (unless I overlooked): with jython, I can execute new code in the context of a running jvm. You can't do that with java.
    – GhostCat
    Commented Feb 10, 2016 at 10:57
  • 1
    I could not understand why this questin is closed . Commented Sep 7, 2017 at 8:32
  • Closed as " ..this question will likely solicit debate, arguments, polling, or extended discussion ..". Yes, technology/programming is area where one shall always go by theory/set standards; and shall avoid discussions, polling, and may be even thinking. Totally make sense!!
    – Raúl
    Commented Dec 12, 2018 at 8:55

14 Answers 14

38

A quick example (from http://coreygoldberg.blogspot.com/2008/09/python-vs-java-http-get-request.html) :

You have a back end in Java, and you need to perform HTTP GET resquests.

Natively :

import java.net.*;
import java.io.*;

public class JGet {
    public static void main (String[] args) throws IOException {
        try {
            URL url = new URL("http://www.google.com");

            BufferedReader in = 
                new BufferedReader(new InputStreamReader(url.openStream()));
            String str;

            while ((str = in.readLine()) != null) {
                System.out.println(str);
            }

            in.close();
        } 
        catch (MalformedURLException e) {} 
        catch (IOException e) {}
    }
}

In Python :

import urllib
print urllib.urlopen('http://www.google.com').read()

Jython allows you to use the java robustness and when needed, the Python clarity.

What else ? As Georges would say...

6
  • 14
    Nothing stop Java from having an utility class that do UrlUtil.open("www.google.com").read(). Commented Sep 22, 2008 at 1:39
  • 24
    Yeah, you just have to code it yourself. Like everything else.
    – Bite code
    Commented Sep 25, 2008 at 19:45
  • 8
    The Corey Goldberg example comparing Java to Python is about as bogus a comparison as can be found on the internet, and the example above isn't much better. (Also, it hints at a robustness/clarity dichotomy which is debatable.) The Java code above does much more than the Python code (OO, buffered reading, exception framework, etc.). There are reasons to use Jython, but a contrived example of brevity isn't very persuasive.
    – Glenn
    Commented May 23, 2009 at 21:30
  • 7
    First, the java code does not do so much more. The Python code is full OO and the open() wrap a file object that does streaming. Plus, I don't see anything about a framework here. SO yes, there is exception handling, but you can do it in Python too. The real trouble here is that java just not let you do it in any shorter way, even with less features. It's not an attack against Java. My point is just : you would use Jython because sometimes, you want something thiner.
    – Bite code
    Commented May 24, 2009 at 0:49
  • 1
    Your are making my point: this s why you would use Jython and not just Java.
    – Bite code
    Commented Dec 23, 2011 at 22:30
19

Python syntax (used by Jython) is considerably more concise and quicker to develop for many programmers.

In addition, you can use existing Python libraries in a Java application.

9

Analogy: Why drink coffee when you can instead drink hot tap water and chew on roasted bitter beans. :-)

For some tasks, Python just tastes better, works better, and is fast enough (takes time to brew?). If your programming or deployment environment is focused on the JVM, Jython lets you code Python but without changing your deployment and runtime enviroment.

6

I've just discovered Jython and, as a bit of a linguist, I think I'd say it's a bit like asking "why use Latin when you can use French" (forgetting about the fact that Latin came before French, of course).

Different human languages actually make you think in different ways. French is a great language, I've lived in France a long time and done a degree in it. But Latin's startling power and concision puts your mind into a different zone, where word order can be swapped around to produce all sorts of subtle effects, for example.

I think, from my cursory acquaintance with Jython, which has really fired up my enthusiasm by the way, that it's going to make me think in different ways. I was very sceptical about Python/Jython for some time, and have been a big fan of Java generics for example (which ironically reduce the amount of typing and therefore "latinise" the French if you like). I don't quite understand the full implications of "dynamically typed" languages like Jython, but I think the best thing is to go with the flow and see what Jython does to my mind!

It's funny how languages come and go. Another "Latin" might be considered to be Algol68 with its infinitely recursive syntax. But the need to develop massively recursive code, and the capacity to read it and think in it, has not (yet) made itself felt. Jython seems to be a very powerful and elegant fit with where we are now, with OO libraries, the power of Java swing, and everything wrapped up in a very elegant bundle. Maybe one day Jython will adopt infinitely recursive syntax too?

5

I use Jython for interactive testing of Java code. This is often much faster than writing Java test applications or even any scripting language. I can just play with the methods and see how it reacts. From there I can learn enough to go and write some real code or test cases.

4

Using Python is more than "syntactic sugar" unless you enjoy writing (or having your IDE generate) hundreds of lines of boiler plate code. There's the advantage of Rapid Development techniques when using dynamically typed languages, though the disadvantage is that it complicates your API and integration because you no longer have a homogeneous codebase. This can also affect maintenance because not everybody on your team loves Python as much as you and won't be as efficient with it. That can be a problem.

4

Some tasks are easier in some languages then others. If I had to parse some file, I'd choose Python over Java in a blink.

1
  • 1
    I needed to read a raw file, and parse out key bits of data. Embedding jython into the middle of my java app let me easily scan the file using regular expressions and list comprehensions, and then return my findings to my java code.
    – gregturn
    Commented Apr 30, 2009 at 17:04
2

Python has some features of functional programming, such as lambdas. Java does not have such functionality, and some programs would be considerably easier to write if such support was available. Thus it is sometimes easier to write the code in Python and integrate it via Jython that to attempt to write the code in Java.

0
2

Python libraries ;) For example BeautifulSoup - an HTML parser that accepts incorrect markup. AFAIK there is no similar pure Java lib.

2
  • Are you sure that it works under Jython? Commented Sep 18, 2008 at 21:15
  • You're right. The stable release is Python 2.2 equivalent. It might too old... and Jython 2.5 is still in alpha state. After the stable release is available it should work though.
    – sumek
    Commented Sep 18, 2008 at 21:20
1

No need to compile. Maybe you want to get something rolling faster than using a compiled language, like a prototype.

...and you can embed the Jython interpreter into your apps. Nice feature, I can't say I've used it, but tis cool nonetheless.

1

Jython can also be used as an embedded scripting language within a Java program. You may find it useful at some point to write something with a built in extension language. If working with Java Jython is an option for this (Groovy is another).

I have mainly used Jython for exploratory programming on Java systems. I could import parts of the application and poke around the API to see what happened by invoking calls from an interactive Jython session.

0

Syntax sugar.

1
  • 4
    Yeah. Real programmers use a magnetized needle and a steady hand (c) xkcd.com/378 . Everything else is a syntax sugar.
    – jfs
    Commented Nov 3, 2008 at 17:45
0

In your situation, it doesn't make much sense. But that doesn't mean it never does. For example, if you are developing a product that allows end users to create extensions or plugins, it might be nice for that to be scriptable.

0

Porting existing code to a new environment may be one reason. Some of your business logic and domain functionality may exist in Python, and the group that writes that code insists on using Python. But the group that deploys and maintains it may want the managability of a J2EE cluster for the scale. You can wrap the logic in Jython in a EAR/WAR and then the deployment group is just seeing another J2EE bundle to be managed like all the other J2EE bundles.

i.e. it is a means to deal with an impedance mismatch.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.