Skip to main content

From Python Hero to Java Rockstar

Are you a Python Hero and do you want to learn more about Java? Then continue reading! I will discuss issues I’ve seen from a Pythonic viewpoint when I was learning Java. I will also talk about why Java is preferred over Python in some cases.

Why Java at all?

I know, Python is easy to understand and easy to learn. So why should you bother to learn Java at all? In some cases, speed is preferred over understandability. In some projects, all the libraries are written in Java and then it is easier to just use Java than to convert all libraries to Python or to find similar packages in Python. If you look at speed for instance, then it would be best to write all applications in Assembly or some other low-level programming language. However, understandability drops quite a few points here. There are not that many developers that can get the gist while reading over Assembly code. Java seems to form a good trade-off between speed and readability. Therefore, it could be the case that some projects go for a Java approach.

There is one foundation which relies almost in all projects on Java. That foundation is Apache. All the Big-Data solutions of Apache are build upon Java. If you are a developer working on a Big-Data project, there is a high probability that you need to write Java code. Since Apache creates open-sourced solutions and since Apache is a well-known foundation, many companies depend on Apache nowadays.

The Package Manager

With Python, most project dependencies are installed using pip. In Java, most projects use Maven as a package manager. In Python, you will often encounter a “requirements.txt” file listing all the dependencies and in Java you will see a “pom.xml” file which lists all the dependencies.


Python is easy. You will just type the following:

cash = 1000

And a variable “cash” is created and initialized with value 1000. Now look at the following Python code:

cash = "a lot"

Now it gets interesting. First, I initialized “cash” with 1000 (a number) and in this piece of code I set the contents of “cash” to “a lot” (a string). Python doesn’t bother variable types. Java is strict on variable types and you need to tell Java which types you are using:

int cash = 1000;

You cannot overwrite the contents of this variable with a string! The variable type cannot be changed at all. To initialize another variable with a string, you would use the following piece of Java code:

String cashString = "a lot";

In Python, you are also free to use either ‘ or ” for strings. In Java, you can only use ” for strings.


Lists are not as flexible as in Python! It can be a pain when you encounter lists for the first time in Java, but remember that Java is speed efficient. Let’s take a look at the following difference. In Python you would write the following:

mylist = [1, 2, 3]

In Java, this would be equivalent to the following:

List<Integer> mylist = new ArrayList<>();

In Java, you also need to specify the type of the elements. As you can see, the list is intended for Integers. Now take a look at the following Python code:

mylist = [1, "two", 3]

Now things get hard in Java! You can define an ArrayList with “Object” elements (all types are subtypes of Object), but it would be a mess when fetching items from this list!

List<Object> mylist = new ArrayList<>();

// Fetch the element 1:
int element1 = (Integer) mylist.get(0);

// Now fetch the "two" element:
String element2 = (String) mylist.get(1);

In Python, you don’t have to specify types and this simplifies the process of list and dictionary creation a lot.


The Pythonic way for creating dictionaries is the following:

mydict = {
    "first": "I am the first element",
    "second": "I am the second element"

In Java, you would use maps for this purpose:

Map<String, String> mydict = new HashMap<>();
mydict.put("first", "I am the first element");
mydict.put("second", "I am the second element");

Notice also that the types need to be specified. In that way, Python is more flexible (notation-wise) than Java.

Conclusions (TL;DR)

Many projects depend on Java. Java is often faster than Python for most solutions, but the notation in Python is more flexible that the notation in Java. In the follow-up of this article, I will discuss higher-level structures of both languages.

Kevin Jacobs

Kevin Jacobs

Kevin Jacobs is a certified Data Scientist and blog writer for Data Blogger. He is passionate about any project that involves large amounts of data and statistical data analysis. Kevin can be reached using Twitter (@kmjjacobs), LinkedIn or via e-mail: