Helping ordinary people create extraordinary websites!
HOME TUTORIALS SCRIPTS WEB HOSTING BLOG FORUM
Get Our Newsletter
Email:

Python and Java - A Side by Side Comparison

By Steve Ferg
2008-01-13


Java vs. Python Productivity - an Overview

There are three main language characteristics that make programmers more productive with Python than with Java.












Java Python
statically typed

In Java, all variable names (along with their types) must be explicitly declared. Attempting to assign an object of the wrong type to a variable name triggers a type exception. That's what it means to say that Java is a statically typed language.

Java container objects (e.g. Vector and ArrayList) hold objects of the generic type Object, but cannot hold primitives such as int. To store an int in a Vector, you must first convert the int to an Integer. When you retrieve an object from a container, it doesn't remember its type, and must be explicitly cast to the desired type.

dynamically typed

In Python, you never declare anything. An assignment statement binds a name to an object, and the object can be of any type. If a name is assigned to an object of one type, it may later be assigned to an object of a different type. That's what it means to say that Python is a dynamically typed language.

Python container objects (e.g. lists and dictionaries) can hold objects of any type, including numbers and lists. When you retrieve an object from a container, it remembers its type, so no casting is required.

For more information on static vs. dynamic typing, see the appendix.

verbose "abounding in words; using or containing more words than are necessary" concise (aka terse) "expressing much in a few words. Implies clean-cut brevity, attained by excision of the superfluous"
not compact compact

In The New Hacker's Dictionary, Eric S. Raymond gives the following definition for "compact":

Compact adj. Of a design, describes the valuable property that it can all be apprehended at once in one's head. This generally means the thing created from the design can be used with greater facility and fewer errors than an equivalent tool that is not compact.

Example

The classic "Hello, world!" program illustrates the relative verbosity of Java.

Java Python
public class HelloWorld
{
public static void main (String[] args)
{
System.out.println("Hello, world!");
}
}
print "Hello, world!"

Example

In the following example, we initialize an integer to zero, then convert it to a string, then check to see if it is empty. Note the data declaration (highlighted), which is necessary in Java but not in Python. Notice also how verbose Java is, even in an operation as basic as comparing two strings for equality.

Java Python
int    myCounter = 0;
String myString = String.valueOf(myCounter);
if (myString.equals("0")) ...
myCounter = 0
myString = str(myCounter)
if myString == "0": ...
// print the integers from 1 to 9
for (int i = 1; i < 10; i++) {
System.out.println(i);
}
# print the integers from 1 to 9
for i in range(1,10):
print i

Example

Your application has 15 classes. (More precisely, it has 15 top-level public classes.)

Java Python
Each top-level public class must be defined in its own file. If your application has 15 such classes, it has 15 files. Multiple classes can be defined in a single file. If your application has 15 classes, the entire application could be stored in a single file, although you would probably want to partition it sensibly into perhaps 4, 5, or 6 files.

Example

In your application, method A calls B calls C calls D calls E calls F. You discover that F must throw exception SpecialException, and it must be caught by A.

Java Python
You must throw SpecialException in F, and catch it in A.
and
You must add "throws SpecialException" to the signatures of methods B, C, D, E, and F.
You must raise SpecialException in F, and catch it in A.

Exceptions will propagate upward automatically; there is nothing more that you must do.

The reason for this is that Java, virtually alone among object-oriented programming languages, uses checked exceptions — exceptions that must be caught or thrown by every method in which they might appear, or the code will fail to compile. Recently (as of June 2003) there seems to be an increasing amount of unhappiness with Java's use of checked exceptions. See Bruce Eckel's "Does Java need Checked Exceptions?" and Ron Waldhoff's "Java's checked exceptions were a mistake".

As chromatic, the Technical Editor of the O'Reilly Network, put it:

I like the idea of checked exceptions in some situations, but forcing every method to deal with (catching or throwing) all exceptions that its child calls or may call can be tedious. I'd rather be able to ignore an exception and let it propagate upwards. Sometimes, I'd rather not worry about exceptions at all.

Example

Your application has an Employee class. When an instance of Employee is created, the constructor may be passed one, two, or three arguments.

If you are programming in Java, this means that you write three constructors, with three different signatures. If you are programming in Python, you write only a single constructor, with default values for the optional arguments.

Java Python
public class Employee
{
private String myEmployeeName;
private int myTaxDeductions = 1;
private String myMaritalStatus =
"single";

//--------- constructor #1 ------
public Employee(String EmployeName)
{
this(employeeName, 1);
}

//--------- constructor #2 ------
public Employee(String EmployeName,
int taxDeductions)
{
this(employeeName, taxDeductions,
"single");
}

//--------- constructor #3 ------
public Employee(String EmployeName,
int taxDeductions,
String maritalStatus)
{
this.employeeName = employeeName;
this.taxDeductions = taxDeductions;
this.maritalStatus = maritalStatus;
}
...
class Employee():

def __init__(self,
employeeName, taxDeductions=1,
maritalStatus="single"):

self.employeeName = employeeName
self.taxDeductions = taxDeductions
self.maritalStatus = maritalStatus
...






In Python, a class has only one constructor. The constructor method is simply another method of the class, but one that has a special name: __init__

Example

In Why Python? Eric S. Raymond notes that:

Python ... is compact — you can hold its entire feature set (and at least a concept index of its libraries) in your head.

In Why I Love Python Bruce Eckel notes that Java is not compact.

I can remember many Python idioms because they're simpler. That's one more reason I program faster [in Python]. I still have to look up how to open a file every time I do it in Java. In fact, most things in Java require me to look something up.
Java Python
import java.io.*;

...

BufferedReader myFile =
new BufferedReader(
new FileReader(argFilename));
# open an input file
myFile = open(argFilename)

Example

Java's string-handling capabilities are surprisingly weak. (But they have improved considerably with the addition of the split method to the String class in Java 1.4.)

Function or Method Java Python
Remove leading and trailing whitespace from string s s.trim() s.strip()
Remove leading whitespace from string s
(not available)
s.lstrip()
Remove trailing whitespace from string s
(not available)
s.rstrip()

Example

Code to add an int to a Vector, and then retrieve it.

Prior to Java 1.5, a new Integer object had to be created and initialized from the int before it could be added to a Vector. In order to retrieve the value, the member of the Vector had to be cast back to an Integer, and then converted back to an int.

Java (before version 1.5) Python
public Vector aList        = new Vector;
public int aNumber = 5;
public int anotherNumber;

aList.addElement(new Integer(aNumber));
anotherNumber =
((Integer)aList.getElement(0)).intValue();
aList = []
aNumber = 5


aList.append(aNumber)
anotherNumber = aList[0]

This clumsiness was eliminated in Java 1.5 with the introduction of generics (which allows you to "type" a container object) and autoboxing (which automates conversion between primitive types and their corresponding wrapper classes). With generics, it is possible to code ContainerType ( which reads as ContainerType restricted to objects of ContainedType).

Java (after version 1.5) Python
public Vector aList = new Vector;
public int aNumber = 5;
public int anotherNumber;

aList.addElement(aNumber);
anotherNumber = aList.getElement(0);
aList = []
aNumber = 5


aList.append(aNumber)
anotherNumber = aList[0]

Example

Verbosity is not just a matter of increasing the number of characters that must be typed — it is also a matter of increasing the number of places where mistakes can be made. The Java code on the left has 5 control characters: ( ) { } ; where the corresponding Python code has only one control character, the colon. (Or two, if you count indentation. See below.)

Java Python
if ( a > b )
{
a = b;
b = c;
}
if  a > b :
a = b
b = c

Omitting or duplicating such characters is easy to do accidentally, and constitutes a severe error in the code. In my personal estimate, I spend 5 times as much time fixing such errors in Java as I do in Python. It really cuts into your productivity — and your creative energy — when you spend that much of your time just trying to satisfy the compiler.

Technically, Python has another control character that Java does not — indentation. But the requirement for correct indentation is the same in Java as it is in Python, because in both languages correct indentation is a practical requirement for human-readable code. The Python interpreter automatically enforces correct indentation, whereas the Java compiler does not. With Java, you need an add-on product such as the Jalopy code formatter to provide automated enforcement of indentation standards.



Tutorial Pages:
» Python vs Java
» Java vs. Python Productivity - an Overview
» Acknowledgments


 | Bookmark
Related Tutorials:
» Learn Python in 10 Minutes
» Python 201 - (Slightly) Advanced Python Topics
» Python 101 - Introduction to Python
» Google Sitemaps
» Python 101
» Python vs. Perl