COMS W4111.001-Introduction to Databases
Fall 2023

Java and Python

Java-to-Python Primer

This document is a very quick primer that compares the basic constructs and data structures of Java and Python 3, which is the version that you should use for the project.

Python is a great language for this course because it requires much less boilerplate code than Java, it has very lightweight servers (unlike J2EE), and it has excellent packages, a REPL, and an incredible wealth of documentation and tutorials for the language.

To get started with Python, please refer to The Python Tutorial.

Main Program Structure

Java main classes have a special method called main that denotes an executable class. They are also typically defined in a file with the same name as the class name. For example, the file HelloWorld.java may contain class HelloWorld:

class HelloWorld {
 public static void main(String[] args) {
 System.out.println("Hello World");
 }
}

In contrast, every Python file is a script that can be run by typing python <filename> in the shell. The script is then executed from top to bottom. For example, helloworld.py may simply look like this:

print ("Hello World")

Functions

Java requires that functions be defined as part of class definitions:

class HelloWorld {
public void f() {
System.out.println("Hello World");
}
}

In Python, functions can be directly defined, independently of class definitions:

def f():
  print ("Hello World")

This means you can then directly call f as f().

Indentation

In Java, statement grouping is defined by using curly braces { }. In contrast, Python groups statements based on indentation level. This is why in the above function definition, the print ("Hello World") statement is indented—so it is grouped as part of the def f() statement. You will see this for the other control structures below.

Control structures

If statements

Java:

int n = 0;
if (1 > 1) {
n = 1;
} else if (1 < 1) {
n = 2;
} else {
n = 3;
}

Python:

n = 0
if 1 > 1:
  n = 1
elif 1 < 1:
  n = 2
else:
  n = 3

Switch statements

Java:

switch(i) {
case 0:
i = 0;
break;
case 1:
i = 1;
break;
default:
i = 2;
}

Python doesn't have a switch statement, so you need to use if/elif/else:

if i == 0:
  i = 0
elif i == 1:
  i = 1
else:
  i = 2

For loops

Java has a for loop:

for (int i = 0; i < 100; i++) {
}

The above can be mechanically translated to Python as follows:

i = 0
while i < 100:
  # note: python doesn't have the ++ shorthand,
  # but has the += shorthand
  i += 1

Java has a more convenient for loop for iterables:

List<Integer> a_list = ...;
 for (int i : a_list) {
 System.out.println(i);
}

Python's for loop is similar:

l = [0, 1, 2]
for i in l:
  print (i)

But Python also has a powerful for-comprehension capability:

l = [0, 1, 2]
r = range(3)
# l and list(r) contain the same values

# prints: 0, 1, 2
for i in l:
  print (i)

# prints: [0, 2, 4]
l2 = [i*2 for i in l]
print (l2)

# prints: [0, 2, 4]
print ([i*2 for i in r])

The for-comprehension also accepts Boolean conditions in the if clause:

# prints: [0, 4]
print ([i*2 for i in range(3) if i != 1])

Maps and dictionaries

Java has maps that map keys to values:

Map<String, String> map = new HashMap<String, String>();
map.put("hello", "world");

Python has a similar data structure called a dictionary. There are multiple ways to instantiate it. Notice that you can directly create a data structure with initial data without resorting to .put() calls:

# Method 1
d1 = dict()
d1["hello"] = "world"

# Method 2, passing key values in constructor
d2 = dict(hello="world")
  
# Method 3, using {}
d3 = {}
d3["hello"] = "world"
  
# Method 4, initializing {} (note that the key has string quotes around it)
d4 = {"hello": "world"}

Dictionary entries can be accessed easily:

# "world"
print (d4["hello"])

Lists

Instead of arrays, Python usually uses lists, which are similar to an ArrayList in Java. The semantics of the two are much the same (i.e., in how we access them), but Python's lists can have variable length and store values of different types. Python uses the following syntax for lists:

l1 = [0, 1, 2]

Python also has a useful range() function:

# [0, 1, 2]
print (list(range(3)))
  
# [0, 2, 4]
print (list(range(0, 5, 2)))
  
# [0, 1, 2]
print ([0, 1, 2])