views:

2246

answers:

14

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?

A: 

Syntax sugar.

Lucas S.
Yeah. Real programmers use a magnetized needle and a steady hand (c) http://xkcd.com/378/ . Everything else is a syntax sugar.
J.F. Sebastian
+10  A: 

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.

Ben Hoffstein
+1  A: 

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.

typicalrunt
A: 

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.

Frank Pape
+4  A: 

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

sumek
Are you sure that it works under Jython?
Cristian Ciupitu
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
+2  A: 

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.

toluju
+3  A: 

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.

Marcio Aguiar
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
+2  A: 

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.

Loren Segal
+1  A: 

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.

ConcernedOfTunbridgeWells
+17  A: 

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

e-satis
Nothing stop Java from having an utility class that do UrlUtil.open("www.google.com").read().
Marcio Aguiar
Yeah, you just have to code it yourself. Like everything else.
e-satis
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
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.
e-satis
+3  A: 

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.

John Meagher
A: 

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.

shemnon
A: 

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.

Aaron
+1  A: 

If you write your application code in Jython rather than Java, unit tests are easier to write because it is easier to mock Jython objects than Java objects. Take a look at these blog posts I made for more details:

http://visionandexecution.org/?p=8

http://visionandexecution.org/?p=33

Also, if you buy into the theory that aggregation should be favored over inheritance, there are some Jython tricks you can do to make aggregation almost as easy as inheritance. I describe these tricks in the following blog post:

http://visionandexecution.org/?p=46

Clint Miller