Python to Java - A Survivalist's Guide

Warning: This article is yet to be completed.

Aim

This guide was written originally for Info1903 students at the University of Sydney who after two terms of Python were told to quickly transfer to Java. This is in no way a replacement for a full illustrative Java textbook however and is merely here to allow an easy transition from Python to Java. In here we'll walk you through a few simple tasks - first a way to print primes.

An Introduction through Prime Numbers

Some important notes

Down to business...

The Ugly Bits

Primitives and Autoboxing

If you didn't know, all the variables in Python are classes - string for example is a class containing your string and also a bunch of functions to use with your string (such as startswith, upper etc). This isn't true in Java however, as there are things called primitives that store just what you put in. Why is this a problem? Well, in things like Collections (such as ArrayLists) they only accept classes and it won't accept primitives. How are we to fix this? For every primitve there is something called a wrapper class which is a class version of the primitive. To differentiate between the two the primitives are always lower case whilst their wrappers are always uppercase (as classes are always uppercase in Java). Autoboxing was introduced to Java to make the transition between primitives and their wrappers simplistic - you'll see an example below.


Both the int and Integer hold 42 - but one gives you methods to use with it and one doesn't
int => Integer
char => Character
none => String
boolean => Boolean

//Autoboxing
Integer x = 42;
//Without autoboxing the above becomes ...
Integer y = new Integer(42);
//You can also do all your normal operations with ints instead of Integers
x -= 4;
y -= new Integer(4);
Most of the time you don't have to worry about primitives and wrapper classes but when using certain datastructures it becomes very important to have an understanding of them.

Data Structures

In this section I'll introduce Java's equivalent data structures to some of Python's.

List

Python's list is really a simple linked list - Java has two similar data structures. First is an array, which is a list with only a set number of 'slots', and second is an ArrayList, which is the closest to Python's list. Notice that for both you need to declare what type of variables it's going to be holding.

/* Array */
String[] numbers = {"one", "two", "three"}; //Creates an array of strings that holds one to three
String[] names = new String[10]; //Creates an array named 'names' with enough space for ten strings
names[0] = "Smerity";
names[1] = "Josh";
int i = 0;
//Any unused slots are by default null
while(names[i] != null)
	System.out.println(names[i++]);
//Can also use the length of an array
for(i=0; i<numbers.length; i++)
	System.out.println(numbers[i]);

/* ArrayList */
ArrayList<Integer> primes = new ArrayList<Integer>();
primes.add(2);
primes.add(3);
primes.add(5);
for(i=0; i<primes.size(); i++)
	System.out.println(primes.get(i));
//Similar to Python's "for num in numbers:" syntax
for(Integer prime : primes)
	System.out.println(prime);

Dictionary

Dictionaries are one of the most useful of Python's default data structures and is widely used. Java of course has similar structures.

/*HashMap<key_type, value_type>*/
//Once again you actually have to say what the types are due to static typing
HashMap<Character, Integer> letters = new HashMap<Character, Integer>();
//.toCharArray turns "tree" into a Character array of {'t', 'r', 'e', 'e'}
for(Character c : "tree".toCharArray()){
	if (!letters.containsKey(c)) letters.put(c, 0);
	letters.put(c, letters.get(c)+1);
}
for(Character c : letters.keySet())
	System.out.println("Letter "+c+" used "+letters.get(c)+" times.");
/*Output -
Letter e used 2 times.
Letter t used 1 times.
Letter r used 1 times.
*/

Comments

Comments yet to load...