Python compared to Java

(updated to reflect a few new things in later Java versions; Jdk 1.4 and 1.5)

How do Java *** concepts translate into their Python analogies? This page sums up the similarities and differences between Java and Python. First you can see the language- and other similarities, after that you see the differences.

Python programs are generally expected to run slower than Java programs, but they also take much less time to develop. Python programs are typically 3-5 times shorter than equivalent Java programs. This difference can be attributed to Python's built-in high-level data types and its dynamic typing. For example, a Python programmer wastes no time declaring the types of arguments or variables, and Python's powerful polymorphic list and dictionary types, for which rich syntactic support is built straight into the language, find a use in almost every Python program. Because of the run-time typing, Python's run time must work harder than Java's. For example, when evaluating the expression a+b, it must first inspect the objects a and b to find out their type, which is not known at compile time. It then invokes the appropriate addition operation, which may be an overloaded user-defined method. Java, on the other hand, can perform an efficient integer or floating point addition, but requires variable declarations for a and b, and does not allow overloading of the + operator for instances of user-defined classes.

For these reasons, Python is much better suited as a "glue" language, while Java is better characterized as a low-level implementation language. In fact, the two together make an excellent combination. Components can be developed in Java and combined to form applications in Python; Python can also be used to prototype components until their design can be "hardened" in a Java implementation. To support this type of development, a Python implementation written in Java is available called Jython, which allows calling Python code from Java and vice versa. In this implementation, Python source code is translated to Java bytecode (with help from a run-time library to support Python's dynamic semantics).

Most similarities and differences are explained in more detail in the other pages on this website, start with PythonForJavaProgrammers.

Overview of the similarities

language similarities

Java

Python

package (directory)

package (directory) with _ _init_ _.py

class source file

module source file

class

class or module (depending on the kind of class it is in java)

strongly typed

strongly typed (objects have a specific type, always. More about this.

boolean, byte, char, short, int, long, float, double (primitive types)

bool, int, str(length 1), int, int, long, float, float (simple types)

Strings are Unicode

strings can be unicode, see UnicodeStrings

BigInteger class

long simple type

BigDecimal class

decimal.Decimal

structured exception handling

structured exception handling

reflection / introspection

reflection / introspection on steroids

iterators (better in Jdk 1.5)

iterators, but better and more generic

other similarities

Java

Python

javac compiler

python compiler/interpreter

java byte code interpreter

python compiler/interpreter

java JIT compiler

Psyco JIT compiler

fully Unicode aware by default

fully Unicode aware if you program to it

write once source, run anywhere

write once run anywhere

object serialization mechanism

marshal and pickle modules

mark-and-sweep garbage collector

reference counting garbage collector with cycle detection

available on many platforms

available on even more platforms

easy multithreading

fairly easy multithreading, but has the sometimes dreaded GIL

XML / XSL processing

XML / XSL processing through various implementations

JNI Java Native Interface

Python C/C++ Extension API (much better!)

JRE Java Runtime classes (rt.jar)

Python standard library (arguably more diverse, although in Jdk1.5 things are improving for Java)

AWT/Swing/SWT GUI toolkits

Tk (standard), wxWindows, MFC, Mac, Gtk, Qt GUI toolkits

Various very good IDEs availble

various IDEs such as Wing IDE, Blackadder, Komodo, PyDev (Eclipse plugin)

J2EE standard

no real corresponding standard, but a lot of APIs from J2EE can readily be found in the Python standard library; see also PEAK, a project to bring other J2EE features to Python

J2EE JSP/Servlet dynamic web pages

Lots of free tools available, no real standard.

J2EE Web Application Server such as Websphere or JBoss

Lots of free tools available, no real standard.

RMI and CORBA

My own Pyro, OmniOrb

JAR

EGG

comp.lang.java.* newsgroups

comp.lang.python and comp.lang.python.announce newsgroups

Overview of the differences

language differences

Java

Python

static typing

dynamic typing (more about this)

variable assignment with typed objects

name binding to typed objects

interfaces

abstract base classes or mixins; also, frameworks such as Twisted, Zope and PyProtocols offer tools for interface declaration, introspection and adaptation.

pointers (sort of)

true object references

{...} code blocks

indentation signifies code blocks; whitespace is relevant; no {...} or BEGIN...END needed

Available through standard packages

Very high level data structures built-in (lists, tuples, sets, maps)

3rd-party support (Apache Commons), albeit more difficult to use

complex number datatype

not present

functions

not present

operator overloading

not possible

multiple inheritance with well-defined Method Resolution Order

Late binding standard (Methods are virtual by default). Methods may be resolved at compile time (early) if declared final or static.

late binding

not present

meta classes

much weaker form of closures are achieved by anonymous classes

lambda functions (unnamed function objects)

not present

list comprehensions such as [x*2 for x in range(5)] gives the list [0, 2, 4, 6, 8]

not present

generators (functions that save state between different results)

Collections API. Generic collections are still typed, but if the collection is unconstrained, it may hold any type (note that the Java compiler does issue warnings; this is not the standard way of doing things in Java)

not needed in Python; most collections (data structures) are built-in, and generic. That is, collections can hold objects of any type.

public, private, protected, package level access

public by default, weak private(is about equivalent with protected) by single leading underscore (e.g. obj._foo), strong private(is practically equivalent with private) by double leading underscores

other differences

Java

Python

no real analogy (although there is BeanShell)

interactive interpreter shell

Always compiled to bytecode

Always compiled to bytecode; not always saved to a file

JIT compiler for better performance

not available in standard Python, but there's Psyco Psyco].

applets in browsers

no analogy

Many J2EE enterprise standards

lacking enterprise standards, but many powerful implementations exist, and a lot of APIs are already present (and standardized) in the Python standard library

Prior to late 2006, Java was proprietary. Now licensed under the OSI-approved CDDL and FSF-approved GPL. Most standard, proprietary libraries are deprecated, although some some still exist.

Python is free (BSD-style, GPL-compatible licensing) and open source technology

Generally good raw performance

Slower performance, but please read PythonSpeed where some of the issues related to speed are discussed

good multithreading

often good multithreading but beware of the GIL

An important advantage with Python is that it's a good team player, both for embedding in programs written in other languages, and for extending with libraries built in other languages. See for instance UsingPythonWithOtherLanguages. I don't really know how well Java compares here though... Maybe someone could expand on that? Neil Benn writes: For java cross language one of the common areas is COM integration, there are two real main players, commercial is JIntegra (also has some .NET interop) which is actually very good, although it's costly. The other is Jacob which works OK but there are issues when handling some complex types - although it's not as smooth as win32com. There used to be an app (from sun) for converting a java bean into and ActiveX component but that was a long time ago and I think that it has fallen off the radar. Also, there is BSF that apparently allows scripting from a variety of scripting languages.

Python+Java, a perfect couple?

It is possible to use Python and Java together in a very interesting way. Jython is a 100% pure Java implementation of the Python interpreter. You can mix and match Python code and Java code, and use Python to script Java applications! Also, you can use Python from your Java programs, so it is possible to use a J2EE Servlet container such as Tomcat and write your servlets in Python!

PythonComparedToJava (last edited 2008-08-17 15:03:04 by IrmenDeJong)