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.
Are you interested in learning more Python? Order our new "Mastering Pandas" course now on Data Blogger Courses for only
- Learn to visualize data using Pandas
- Learn how to load and store data effectively
- Learn advanced data operations
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:
And a variable “cash” is created and initialized with value 1000. Now look at the following Python code:
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:
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:
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! In Python, it is easy to write mathematical expressions using lists. See this blog post about Python list comprehensions for more information. 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:
In Java, this would be equivalent to the following:
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:
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!
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:
In Java, you would use maps for this purpose:
Notice also that the types need to be specified. In that way, Python is more flexible (notation-wise) than Java.
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.
Help building the Data Blogger CommunityHelp to grow our community to spread AI and Data Science education around the globe.
Every penny counts.